1100 Mars Numbers (20 point(s))
People on Mars count their numbers with base 13:
- Zero on Earth is called "tret" on Mars.
- The numbers 1 to 12 on Earth 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 Nlines 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
一般模拟。需要特别仔细。
踩坑:
1. 注意低位0是唯一一个长度为4的字符表示。最开始用长度来区分的方法不妥。
2. 注意13是表示成tam而不是tam tret。
3. 注意比较cin和getline()。cin输入后回车依然在缓冲区,而getline()会直接去读取,所以需要在cin后getchar()将它消耗。而getline()读入字符串后,回车不会继续保留在缓冲区。参考链接:https://blog.youkuaiyun.com/qq_37131497/article/details/82629440。
#include<iostream>
#include<string>
using namespace std;
string lowDigit[13]={"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov","dec"};
string highDigit[13]={" ","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
int mar2earth(string s){
if(s.length()<=4){
for(int i=0;i<13;i++){
if(s==lowDigit[i]) return i;
if(s==highDigit[i]) return i*13;
}
}
else{
string low_str = s.substr(4,s.size()-4);
string high_str = s.substr(0,3);
int low,high;
for(int i=0;i<13;i++){
if(low_str==lowDigit[i]){
low = i;break;
}
}
for(int i=1;i<13;i++){
if(high_str==highDigit[i]){
high = i;break;
}
}
return high*13+low;
}
}
string earth2mar(int n){
string ans = "";
if(n<=12) ans=lowDigit[n];
else if(n%13==0) ans = highDigit[n/13];
else{
ans+=highDigit[n/13];
ans+=" ";
ans+=lowDigit[n%13];
}
return ans;
}
int string2int(string s){
if(s.length()==1) return s[0]-'0';
else if(s.length()==2) return 10*(s[0]-'0')+(s[1]-'0');
else return 100*(s[0]-'0')+10*(s[1]-'0')+s[2]-'0';
}
int main(void){
int N;string s;
cin>>N;getchar();
while(N--){
getline(cin,s);
if(s[0]>'9'||s[0]<'0') cout<<mar2earth(s)<<endl;
else{
cout<<earth2mar(string2int(s))<<endl;
}
}
return 0;
}