1044. 火星数字(20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
火星人是以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
#include<iostream> #include<vector> #include<cstdio> #include<cstring> #include<string> #include<algorithm> using namespace std; string lowrelation[13] = { "tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" }; string highrelation[13]= { "","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" }; struct Mars { string high, low; Mars() :high(""), low("") {}; }; int transform(Mars m) { int sum = 0; for (int i = 0; i < 13; i++) { if (m.high == highrelation[i]) { sum += i * 13; break; } } for (int i = 0; i < 13; i++) { if (m.low == lowrelation[i]) { sum += i; break; } } return sum; } Mars transform(int n) { Mars m; if(n%13!=0||n==0)//这个地方得注意,当数字是13的倍数且不是0时,低位的tret不能被输出 m.low = lowrelation[n % 13]; n /= 13; m.high = highrelation[n]; return m; } void print(int i) { cout << i << endl; } void print(Mars m) { if (m.high != ""&&m.low != "") cout << m.high << " " << m.low << endl; else if (m.high == "") cout << m.low << endl; else cout << m.high << endl; } int stringToInt(string s) { int sum = 0; for (int i = 0; i < s.size(); i++) { sum += (s[i] - '0')*pow(10, s.size() - 1 - i); } return sum; } int main() { int N; cin >> N; string temp; getchar(); while (N--) { getline(cin, temp); if (temp < "999")//数字在字母前面,大写字母在小写前面 { print(transform(stringToInt(temp)));//print、transform利用重写同名函数实现多态性 } else { Mars m; int pos = temp.find(" "); if (pos < 0) { m.low = temp; for (int i = 0; i < 13; i++) { if (m.low == highrelation[i]) { m.high = m.low; m.low = ""; } } } else { m.high = temp.substr(0, pos); m.low = temp.substr(pos + 1, temp.size() - pos-1); } print(transform(m)); } } return 0; }