People on Mars count their numbers with base 13:
- Zero on Earth is called "tret" on Mars.
- The numbers 1 to 12 on Earch is called "jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec" on Mars, respectively.
- For the next higher digit, Mars people name the 12 numbers as "tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou", respectively.
For examples, the number 29 on Earth is called "hel mar" on Mars; and "elo nov" on Mars corresponds to 115 on Earth. In order to help communication between people from these two planets, you are supposed to write a program for mutual translation between Earth and Mars number systems.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (< 100). Then N lines follow, each contains a number in [0, 169), given either in the form of an Earth number, or that of Mars.
Output Specification:
For each number, print in a line the corresponding number in the other language.
Sample Input:
4 29 5 elo nov tam
Sample Output:
hel mar may 115 13
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<map> 5 #include<string> 6 #include<string.h> 7 using namespace std; 8 map<string, int> mp; 9 int str2num(char s[]){ 10 int ans = 0, P = 1, len = strlen(s); 11 for(int i = len - 1; i >= 0; i--){ 12 ans += P * (s[i] - '0'); 13 P *= 10; 14 } 15 return ans; 16 } 17 string low[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"}; 18 string high[13] = {"tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"}; 19 int main(){ 20 string temp; 21 int N; 22 char s[10], s2[10]; 23 for(int i = 1; i < 13; i++){ //建立字符串到数字的映射 24 for(int j = 1; j < 13; j++){ 25 mp[high[i] + " " + low[j]] = i * 13 + j; 26 } 27 } 28 for(int i = 1; i < 13; i++){ //仅有低位或仅有高位 29 mp[high[i]] = i * 13; 30 mp[low[i]] = i; 31 } 32 mp[low[0]] = 0; 33 scanf("%d ", &N); 34 for(int i = 0; i < N; i++){ 35 gets(s); 36 if(s[0] >= 'a' && s[0] <= 'z'){ 37 printf("%d\n", mp[s]); 38 }else{ 39 int a3 = str2num(s); 40 int h = a3 / 13, l = a3 % 13; 41 if(h != 0 && l!= 0) 42 printf("%s %s\n", high[h].c_str(), low[l].c_str()); 43 else if(l == 0 && h != 0) 44 printf("%s\n", high[h].c_str()); 45 else if(l != 0 && h == 0) 46 printf("%s\n", low[l].c_str()); 47 else printf("%s\n", low[l].c_str()); 48 } 49 } 50 cin >> N; 51 return 0; 52 }
总结:
1、本题输入的情况过于复杂,有字母有数字,字母还有可能是2个单词或1个单词,且由于表示的性质,单个单词有可能表示高位,也可能低位。所以最好直接按行读入,枚举所有情况即可(共169个数)。
2、数字转火星文:26应直接转为高位2, 而非高2低0。
3、gets() 函数读取一行字符串,当上一行用scanf读取过一个%d时,需要吸收掉上一行的 \n,否则 gets 会读到空串。