1044. 火星数字(20)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B
判题程序 Standard 作者 CHEN, Yue
火星人是以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
注意点:
- 13的倍数只输出高位,低位0不输出
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
void InitMap(map<string ,int>&lowb,map<string,int>&highb)
{
lowb["tret"] = 0; lowb["jan"] = 1; lowb["feb"] = 2;
lowb["mar"] = 3; lowb["apr"] = 4; lowb["may"] = 5;
lowb["jun"] = 6; lowb["jly"] = 7; lowb["aug"] = 8;
lowb["sep"] = 9; lowb["oct"] = 10; lowb["nov"] = 11;
lowb["dec"] = 12;
highb["tret"] = 0;
highb["tam"] = 1; highb["hel"] = 2;highb["maa"] = 3;
highb["huh"] = 4; highb["tou"] = 5;highb["kes"] = 6;
highb["hei"] = 7; highb["elo"] = 8;highb["syy"] = 9;
highb["lok"] = 10; highb["mer"] = 11;highb["jou"] = 12;
}
void InitString(string lowN[],string highN[])
{
lowN[0] = "tret"; lowN[1] = "jan"; lowN[2] = "feb";
lowN[3] = "mar"; lowN[4] = "apr"; lowN[5] = "may";
lowN[6] = "jun"; lowN[7] = "jly"; lowN[8] = "aug";
lowN[9] = "sep"; lowN[10] = "oct"; lowN[11] = "nov";
lowN[12] = "dec";
highN[0] = "tret";
highN[1] = "tam"; highN[2] = "hel";highN[3] = "maa";
highN[4] = "huh"; highN[5] = "tou";highN[6] = "kes";
highN[7] = "hei"; highN[8] = "elo";highN[9] = "syy";
highN[10] = "lok"; highN[11] = "mer";highN[12] = "jou";
}
enum TYPE {NUM,STR,NONE};
TYPE Judge(string str)
{
if (str.size())
{
if ('0' <= str[0] && str[0] <= '9')
return NUM;
if ('a' <= str[0] && str[0] <= 'z')
return STR;
}
return NONE;
}
void GetBit(string s, int & h, int & l)
{
int num = 0;
for (int i = 0; i < s.size(); ++i)
{
num = 10 * num + s[i] - '0';
}
h = num / 13;
l = num % 13;
}
void GetStrb(string s, string &hb, string & lb, map<string,int>highb)
{
hb.clear(), lb.clear();
int pos = s.find(' ');
if (pos != string::npos)
{
for (int i = 0; i < pos; ++i)
hb += s[i];
for (int i = pos + 1; i < s.size(); ++i)
lb += s[i];
return;
}
if (highb.count(s))
{
hb = s;
return;
}
lb = s;
}
int ToBase13(int h, int l)
{
return h * 13 + l;
}
int main()
{
#ifdef _DEBUG
freopen("data.txt", "r+", stdin);
#endif // _DEBUG
int n;
map<string, int>lowb, highb;
string lowStr[13], highStr[13],in;
string hs, ls;
int h, l;
InitMap(lowb, highb);
InitString(lowStr, highStr);
cin >> n; getchar();
while (n--)
{
getline(cin, in);
TYPE type = Judge(in);
switch (type)
{
case NUM:
GetBit(in, h, l);
if (h > 0)
printf("%s", highStr[h].c_str());
if (h && l)
printf(" ");
if(l > 0)
printf("%s", lowStr[l].c_str());
if (l || h)
printf("\n");
if (l == 0 && h == 0)
printf("tret\n");
break;
case STR:
GetStrb(in, hs, ls, highb);
h = l = 0;
if (hs.size())
h = highb[hs];
if(ls.size())
l = lowb[ls];
printf("%d\n", ToBase13(h,l));
break;
case NONE:
break;
}
}
return 0;
}