1044. 火星数字(20)

题目描述

火星人是以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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <iostream>
#include <cstring>
using namespace std;

string gewei[13] = {"", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string shiwei[13] = {"", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};

void int_to_str(string);
void str_to_int(string);

int main()
{
int n;
string str;

cin >> n;
getchar();
while (n--) {
getline(cin, str);
if (iswdigit(str[0])) {
int_to_str(str);
} else {
str_to_int(str);
}
}

return 0;
}

void int_to_str(string str)
{
int num;
int a, b;

num = stoi(str, nullptr, 0);
if (num == 0) {
cout << "tret" << endl;
return;
}
a = num / 13;
b = num % 13;
if (a != 0) {
cout << shiwei[a];
if (b != 0) {
cout << " " << gewei[b] << endl;;
} else {
cout << endl;
}
} else {
cout << gewei[b] << endl;;
}
}

void str_to_int(string str)
{
int len;
int a, b;

len = str.length();
if (len == 4 && strcmp(str.c_str(), "tret") == 0) {
cout << "0\n";
return;
}
if (len == 3) {
for (a = 1; a < 13; a++) {
if (strcmp(str.c_str(), shiwei[a].c_str()) == 0) {
cout << a * 13 << endl;
return;
}
}
for (b = 1; b < 13; b++) {
if (strcmp(str.c_str(), gewei[b].c_str()) == 0) {
cout << b << endl;
return;
}
}
} else if (len == 7) {
for (a = 1; a < 13; a++) {
if (str[0] == shiwei[a][0] && str[1] == shiwei[a][1] && str[2] == shiwei[a][2]) {
break;
}
}
for (b = 1; b < 13; b++) {
if (str[4] == gewei[b][0] && str[5] == gewei[b][1] && str[6] == gewei[b][2]) {
break;
}
}
cout << a * 13 + b << endl;
}
}