People on Mars count their numbers with base 13:
- Zero on Earth is called "tret" on Mars.
- The numbers 1 to 12 on Earch 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 N lines 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
-----------------------------------这是题目和解题的分割线-----------------------------------
转换为十三进制,实际就是当火星文→数字时,第一个字符串所在下标×13+第二个字符串所在下标。
数字→火星文时,火星文的第一个字符串的下标 = 数字/13 ,第二个 = 数字%13。
注意,13的倍数无论是火星文还是数字都不要输出0。
//打表法
#include<cstdio>
#include<string>
#include<map>
#include<iostream>
using namespace std;
map<string,int> mp;
string lowD[13] = {"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
string highD[13] = {"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
//169的范围很小可以直接打表
void Init()
{
int i;
//小于13无需组合
for(i=0;i<13;i++)
mp[lowD[i]] = i;
for(i=13;i<169;i++)
{
string str;
//13的倍数不需要第二个字符串
if(i%13==0) str = highD[i/13];
//记得中间加" "
else str = highD[i/13]+" "+lowD[i%13];
mp[str] = i;
}
}
int main()
{
int n,i;
cin>>n;
Init();
getchar();
while(n--)
{
string s;
//一行一行读取,因为火星文中间有空格
getline(cin,s);
string newS;
//如果读入的是数字
if(s[0]<='9'&&s[0]>='0')
{
int r = 0;
//先转换为整型
for(int i=0;i<s.size();i++)
r = r*10 + s[i] - '0';
//13的倍数(判断条件得加上r!=0不然会误伤)
if(r!=0&&r%13==0) newS = highD[r/13];
else if(r>13) newS = highD[r/13]+" "+lowD[r%13];
else newS = lowD[r];
cout<<newS<<endl;
}
else cout<<mp[s]<<endl;
}
return 0;
}
//不打表法
#include<cstdio>
#include<string>
#include<cctype>
#include<map>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
int n,i;
string ten[15] = {"","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
string one[15] = {"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
string s;
map<string,int> mp1,mp2;
for(i=0;i<13;i++)
mp1[ten[i]] = i;
for(i=0;i<13;i++)
mp2[one[i]] = i;
cin>>n;
getchar();
for(i=0;i<n;i++)
{
getline(cin,s);
if(isdigit(s[0]))
{
int tmp = stoi(s);
//cout<<tmp<<endl;
if(tmp%13==0&&tmp!=0) cout<<ten[tmp/13]<<endl;
else if(tmp<13) cout<<one[tmp]<<endl;
else cout<<ten[tmp/13]<<" "<<one[tmp%13]<<endl;;
}
else
{
int pos = s.find(" ");
if(pos!=string::npos)
{
string s2 = s.substr(pos+1);
string s1 = s.substr(0,pos);
//cout<<s1<<" "<<s2<<endl;
cout<<mp1[s1]*13+mp2[s2]<<endl;
}
else
{
if(mp1.find(s)!=mp1.end())
cout<<mp1[s]*13<<endl;
else cout<<mp2[s]<<endl;
}
}
}
return 0;
}