People on Mars count their numbers with base 13:
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.
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.
For each number, print in a line the corresponding number in the other language.
4
29
5
elo nov
tam
hel mar
may
115
13
火星文采用了13进制的转换方式,以下给出了火星文低12,高12位的对应转换规则(加上0,是13位):
1 2 3 4 5 6 7 8 9 10 11 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 (高12位)
其中地球上的0在火星上是tret
eg.
“hel mar” (mars)- - - 29(earth)
hel
mar
“elo nov” (mars)- - - 115(earth)
elo
nov
火星文转地球文时,由于不知道其是一位数还是两位数,if(s2 == l[i]||s1==l[i]) n += i;
便很好地解决了这个问题,如果只有一位:便是s1=l[i];如果有两位,便是s2=l[i]。(其中s1是用户输入字符串的高三位,s2是低三位)。
代码如下
#include
#include
using namespace std;
string l[13]={"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string h[13]={"","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
string s;
void etom(int n){
if(n / 13) cout<<h[n/13];
if(n/13 && n%13) cout<<" ";
if(n%13 || n==0) cout<<l[n%13];
}
void mtoe(){
int n = 0;
string s1 = s.substr(0,3),s2;
if(s.length() > 4) s2 = s.substr(4,3);
for(int i = 1; i < 13; i++){
if(s1 == h[i]) n += 13*i;
if(s2 == l[i]||s1==l[i]) n += i;
}
cout<<n;
}
int main()
{
int n;
scanf("%d\n", &n);
for(int i = 0; i < n; i++){
getline(cin, s);
if(isdigit(s[0])){
etom(stoi(s));
}else{
mtoe();
}
cout<<endl;
}
return 0;
}