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 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
分析:如果火星数字的个位为0,则只显示十位数字。注意不要抄错数字的英文表示。
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string mone[] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string mten[] = {"tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
string e2m(int num)
{
if(num == 0)
return "tret";
string result = "";
int ten = num / 13;
int one = num % 13;
if(ten != 0)
result += mten[ten];
if(one != 0 && ten != 0)
result += " ";
if(one != 0)
result += mone[one];
return result;
}
int m2e(string num)
{
if(num.size() == 4) // "tret"
return 0;
int result = 0;
if(num.size() > 4)
{
string ten = num.substr(0, 3);
int i;
for(i = 0; i < 13; i++)
{
if(ten == mten[i])
break;
}
result = i * 13;
string one = num.substr(4, 3);//火星数字的个位
for(i = 0; i < 13; i++)
{
if(one == mone[i])
break;
}
result += i;
}
if(num.size() == 3)
{
int i;
for(i = 0; i < 13; i++)
{
if(num == mten[i])
break;
}
if(i != 13) //只有十位
result = i * 13;
for(i = 0; i < 13; i++)
{
if(num == mone[i])
break;
}
if(i != 13) //只有个位
result = i;
}
return result;
}
int main()
{
int n;
cin >> n;
getchar();
string in;
for(int i = 0; i < n; i++)
{
getline(cin, in);
if(isdigit(in[0]))
{
int num = 0;
for(int j = 0; j < in.size(); j++)
num = num * 10 + (in[j] - '0');
cout << e2m(num) << endl;
}
else
cout << m2e(in) << endl;
}
return 0;
}