1044 火星数字 (20 point(s))
火星人是以 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
经验总结:
emmmmmm 这一题,我觉得难点在......考验眼力啊!! 第一遍编译错误我仔细检查了一下火星文 发现里面hel 和hei 不仔细看真的看不出来啊!还有,注意使用getline()函数,如果前面用过scanf()函数,需要getchar()函数吸收一下换行符,不然geiline()读入的是空字符串,再有,注意整数转换为火星文如果能被13整除,只输出高位的火星文。就这样啦~( •̀ .̫ •́ )✧
AC代码
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <map>
using namespace std;
char low[13][5]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
char high[13][5]={"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
bool judge(string str)
{
return isalpha(str[0])?true:false;
}
int main()
{
string str,temp;
int n;
map<string,int> mp;
for(int i=0;i<13;++i)
{
temp=low[i];
mp[temp]=i;
temp=high[i];
mp[temp]=i*13;
}
while(~scanf("%d",&n))
{
getchar();
for(int i=0;i<n;++i)
{
getline(cin,str);
if(judge(str))
{
int ans=0;
int pos=str.find(' ');
if(pos!=string::npos)
{
ans=mp[str.substr(0,pos)]+mp[str.substr(pos+1,str.size()-pos-1)];
}
else
{
ans=mp[str];
}
printf("%d\n",ans);
}
else
{
int x;
sscanf(str.c_str(),"%d",&x);
if(x<=12)
{
printf("%s\n",low[x]);
}
else
{
if(x%13==0)
printf("%s\n",high[x/13]);
else
printf("%s %s\n",high[x/13],low[x%13]);
}
}
}
}
return 0;
}