PAT B1044/A1100

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进制计数的:

  • 地球人的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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "stdio.h"
//#include "math.h"
#include "string.h"
#include "iostream"
//#include "stdlib.h"
#include "algorithm"
//#include "vector"
//#include "set"
#include "map"
using namespace std;
//typedef long long LL;

//[0,12]火星文
string unitDigit[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun",
"jly", "aug", "sep", "oct", "nov", "dec"};
//13的[0,12]倍火星文
string tenDigit[13] = {"tret", "tam", "hel", "maa", "huh", "tou", "kes",
"hei", "elo", "syy", "lok", "mer", "jou"};
string numToStr[170];//数字->火星文
map<string, int> strToNum;//火星文->数字
void init(){
for (int i = 0; i < 13; i++) {
numToStr[i] = unitDigit[i];//个位为[0,12],十位为0
strToNum[unitDigit[i]] = i;
numToStr[i * 13] = tenDigit[i];//十位为[0,12],个位为0
strToNum[tenDigit[i]] = i * 13;
}
for (int i = 1; i < 13; i++) {//十位
for (int j = 1; j < 13; j++) {//个位
string str = tenDigit[i] + " "+ unitDigit[j];//火星文
numToStr[i * 13 + j] = str;//数字->火星文
strToNum[str] = i * 13 + j;//火星文->数字
}
}
}
int main(){
init();//打表
int T;
scanf("%d%*c", &T);

while (T--) {
string str;
getline(cin, str);
if (str[0] >= '0' && str[0] <= '9') {//如果是数字
int num = 0;//字符串转换成数字
for (int i = 0; i < str.length(); i++) {
num = num * 10 + (str[i] - '0');
}
cout << numToStr[num] << endl;//直接查表
} else {
cout << strToNum[str] << endl;//直接打表
}
}

return 0;
}

%c其中%c是读一个字符,而中间的号是跳过一个字符。意思就是读取一个字符但并不赋值给变量。
如果不添加这个,也可以在scanf后面加一个getchar吸收换行符。
另外这里使用getline是因为字符串有些是有空格分割的,如果用printf(“%s”, str)和cin >> str;都会断开