1100 Mars Numbers (20 分)
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
AC代码
#include <iostream>
#include <string>
#include <cctype>
#include <map>
using namespace std;
void Swi(int x);
void Sec(int x);
int main() {
int N;
cin >> N;
cin.get(); //吃掉队列中的换行符
while (N--) {
string A;
getline(cin, A);
if (!isalpha(A[0])) { //数字转字母情况
if (A.size() == 1) Swi(A[0] - '0'); //如果只有一位数字
else { //多位数字
int Num = 0;
int Ret[3] = { 0 };
for (int i = 0; i < A.size(); i++) Num = Num * 10 + (A[i] - '0'); //得到数字
if (Num >= 13) {
Ret[1] = Num % 13;
Num /= 13;
Ret[0] = Num;
}
else { Ret[0] = Num; Ret[1] = -1; }
if (Ret[1] < 0) Swi(Ret[0]); //转换后只有一位数
else {
Sec(Ret[0]);
if (Ret[1]) { cout << ' '; Swi(Ret[1]); } //转换后个位数不为零
else cout << endl; //转换后个位数为零不输出'tret'
}
}
}
else { //字母转数字情况
map<string, int> mp;
mp["tret"] = 0; mp["jan"] = 1; mp["feb"] = 2; mp["mar"] = 3;
mp["apr"] = 4; mp["may"] = 5; mp["jun"] = 6; mp["jly"] = 7;
mp["aug"] = 8; mp["sep"] = 9; mp["oct"] = 10; mp["nov"] = 11;
mp["dec"] = 12; mp["tam"] = 1 * 13; mp["hel"] = 2 * 13;
mp["maa"] = 3 * 13; mp["huh"] = 4 * 13; mp["tou"] = 5 * 13;
mp["kes"] = 6 * 13; mp["hei"] = 7 * 13; mp["elo"] = 8 * 13;
mp["syy"] = 9 * 13; mp["lok"] = 10 * 13; mp["mer"] = 11 * 13;
mp["jou"] = 12 * 13; //mp中以字母段为键值 数字为实值
if (A == "tret") { cout << "0" << endl; continue; } //为零
if (A.find(' ') != string::npos) { //有两个字母段
string Q(A, 0, 3);
string W(A, 4);
int TheNum = mp[Q] + mp[W]; //得到最终数字
cout << TheNum << endl;
}
else { //只有一个字母段 (包含被13整除的数字)
int TheNum = mp[A];
cout << TheNum << endl; //输出数字 (包含被13整除的数字)
}
}
}
return 0;
}
void Swi(int x) {
switch (x) {
case 0: cout << "tret" << endl; break;
case 1: cout << "jan" << endl; break;
case 2: cout << "feb" << endl; break;
case 3: cout << "mar" << endl; break;
case 4: cout << "apr" << endl; break;
case 5: cout << "may" << endl; break;
case 6: cout << "jun" << endl; break;
case 7: cout << "jly" << endl; break;
case 8: cout << "aug" << endl; break;
case 9: cout << "sep" << endl; break;
case 10: cout << "oct" << endl; break;
case 11: cout << "nov" << endl; break;
case 12: cout << "dec" << endl; break;
};
}
void Sec(int x) {
switch (x) {
case 1: cout << "tam"; break;
case 2: cout << "hel"; break;
case 3: cout << "maa"; break;
case 4: cout << "huh"; break;
case 5: cout << "tou"; break;
case 6: cout << "kes"; break;
case 7: cout << "hei"; break;
case 8: cout << "elo"; break;
case 9: cout << "syy"; break;
case 10: cout << "lok"; break;
case 11: cout << "mer"; break;
case 12: cout << "jou"; break;
};
}