zoukankan      html  css  js  c++  java
  • PAT 乙级 1044 火星数字 (20 分)

    1044 火星数字 (20 分)

    火星人是以 13 进制计数的:

    • 地球人的 0 被火星人称为 tret。
    • 地球人数字 1 到 12 的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。
    • 火星人将进位以后的 12 个高位数字分别称为:tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou。

    例如地球人的数字 29 翻译成火星文就是 hel mar;而火星文 elo nov 对应地球数字 115。为了方便交流,请你编写程序实现地球和火星数字之间的互译。

    输入格式:

    输入第一行给出一个正整数 N(<100),随后 N 行,每行给出一个 [0, 169) 区间内的数字 —— 或者是地球文,或者是火星文。

    输出格式:

    对应输入的每一行,在一行中输出翻译后的另一种语言的数字。

    输入样例:

    4
    29
    5
    elo nov
    tam
    

    输出样例:

    hel mar
    may
    115
    13


    注意:13的倍数情况要进行额外讨论

     1 #include<iostream>
     2 #include<string>
     3 #include<cstdlib>
     4 
     5 using namespace std;
     6 
     7 string str1[13] = { "tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" },
     8 str2[13] = { "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" };
     9 
    10 void earthToMars(string& str)
    11 {
    12     int num = atoi(str.c_str());
    13 
    14     if (num / 13 > 0 && num % 13 != 0)
    15         cout << str2[num / 13 - 1] << " " << str1[num % 13] << endl;
    16     else if (num / 13 > 0 && num % 13 == 0)
    17         cout << str2[num / 13 - 1] << endl;
    18     else
    19         cout << str1[num % 13] << endl;
    20 }
    21 
    22 void marsToEarth(string &str)
    23 {
    24     int sum = 0;
    25 
    26     if (str.size() == 3)
    27     {
    28         for (int i = 0; i<13; ++i)
    29         {
    30             if (str1[i] == str)
    31             {
    32                 cout << i << endl;
    33                 return;
    34             }
    35         }
    36 
    37         for (int i = 0; i<13; ++i)
    38         {
    39             if (str2[i] == str)
    40             {
    41                 cout << (i+1)*13 << endl;
    42                 return;
    43             }
    44         }
    45     }
    46     else
    47     {
    48         string strLeft = str.substr(0, 3);
    49         string strRight = str.substr(4);
    50 
    51         for (int i = 0; i<13; ++i)
    52             if (str2[i] == strLeft)
    53                 sum += (i+1) * 13;
    54 
    55         for (int i = 0; i<13; ++i)
    56             if (str1[i] == strRight)
    57                 sum += i;
    58 
    59         cout << sum << endl;
    60     }
    61 }
    62 
    63 int main()
    64 {
    65     int N;
    66     string str;
    67 
    68     cin >> N;
    69     getchar();
    70 
    71     for (int i = 0; i<N; ++i)
    72     {
    73         getline(cin, str);
    74 
    75         if (str[0] >= 48 && str[0] <= 57)
    76             earthToMars(str);
    77         else
    78             marsToEarth(str);
    79     }
    80 }



  • 相关阅读:
    分类算法 学习笔记
    机器学习概述 & 特征工程 学习笔记
    Java基础知识
    牛客中Java工程师模拟面试整理
    leetcode142. 环形链表 II
    面经中的题目整理
    面经总结
    软件设计师补题(2007下半年上午题)
    软件设计师补题(2005上半年上午题)
    软件设计师补题(2005下半年上午题)
  • 原文地址:https://www.cnblogs.com/cdp1591652208/p/10230747.html
Copyright © 2011-2022 走看看