pat乙级

1044 火星数字(20)(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 #include<iostream> #include<string> #include<map> #include<cstdio> #include<cctype> #include<cstring> using namespace std; map <string, int> mp1, mp2; string num1[13] = { "tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" }; string num2[13] = { "tret","tam", "hel","maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"}; void change(string str) {     for (int i = 0; i < 13; i++)     {         mp1[num1[i]] = i;     }     for (int i = 0; i < 13; i++)     {         mp2[num2[i]] = i;     } //    cout << "进入函数内部" << endl;     int length = str.length();     int sum = 0, s, g;     if (str[0] <= '9'&&str[0] >= '0')     { //        cout << "进入if" << endl;         int i = 0;         while (length)         {             sum = sum * 10 + (str[i] - '0');             i++;             length--;         }         s = sum / 13;         g = sum % 13;         if(g!=0)         str = s != 0 ? num2[s] + " " + num1[g] : num1[g];         else         {             str=num2[s];         }         cout << str<<endl;     }     else     { //        cout<<"str.length():"<<str.length()<<endl;         if (str.length() > 5)         {             string str1 = "",str2="";             str1=str1+str[0]+str[1]+str[2];             str2=str2+str[4]+str[5]+str[6]; //            cout<<str1<<" "<<str2<<endl; //            cout<<mp2[str1]<<" "<<mp1[str2]<<endl;             cout<<mp2[str1]*13+mp1[str2]<<endl;         }         else         {             if (mp1[str] != 0)                 cout << mp1[str]<<endl;             else                 cout << mp2[str] * 13<<endl;         }     } } int main() { int n; scanf("%d",&n); getchar(); while(n--)     {     string str; // cin>>n;     getline(cin, str);     if(str[0]=='0')         cout<<"tret"<<endl;     else if(str=="tret")         cout<<'0'<<endl;     else     change(str);     }     return 0; }

猜你喜欢

转载自blog.csdn.net/qq_42039996/article/details/80626275