PAT A1100 Mars Numbers
- 做了字符串和数字的双向映射,然后就是判断是数字还是字符串,然后做相应的转换或切割
- 同样注意cin和getline同时使用时结尾回车的问题
- 以及当数字是13的倍数时,火星文的个位0(tret)不输出,并处理空格和换行
#include<iostream>
#include<string>
#include<unordered_map>
using namespace std;
string toMars1[] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string toMars2[] = {"tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
unordered_map<string,int> umap1,umap2;
int main(){
int num;
cin >> num;
getchar();
for(int i = 0;i < 13;i ++){
umap1.insert({toMars1[i],i});
umap2.insert({toMars2[i],i});
}
for(int i = 0;i < num;i ++){
string tmp;
getline(cin,tmp);
if(tmp[0] <= '9' && tmp[0] >= '0'){
int val = stoi(tmp);
if(val / 13){
cout << toMars2[val / 13];
if(val % 13 == 0){
cout << endl;
continue;
} else cout << ' ';
}
cout << toMars1[val % 13] << endl;
}else{
int pos = tmp.find(' ');
if(pos != -1){
cout << umap2[tmp.substr(0,pos)] * 13 + umap1[tmp.substr(pos + 1,-1)] << endl;
}else cout << umap2[tmp] * 13 + umap1[tmp] << endl;
}
}
return 0;
}