火星人是以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<cstdio>
#include<string>
#include<iostream>
using namespace std;
int main() {
int n, len, num;
string temp, a;
string a1[13] = { "tret","jan", "feb","mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" };
string a2[12] = { "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" };
cin >> n;
getchar();
for (int i = 0; i < n; i++) {
getline(cin, temp);
len = temp.length(); num = 0;
//数字类型
if (temp[0] >= '0'&&temp[0] <= '9') {
for (int i = 0; i < len; i++)
num = num * 10 + temp[i] - '0';
if (num < 13)
cout << a1[num] << endl;
else {
//为13的整数倍时不需要输出各位
if (num % 13 == 0)
cout << a2[num / 13 - 1] << endl;
else
cout << a2[num / 13 - 1] << " " << a1[num % 13] << endl;
}
}
//火星文类型
else {
for (int i = 0; i < len; i += 4) {
a = "";
a = a + temp[i] + temp[i + 1] + temp[i + 2];
//进行比较
for (int j = 0; j < 13; j++) {
if (a == a1[j]) {
num += j;
break;
}
if (a == a2[j]) {
num += (j + 1) * 13;
break;
}
}
}
cout << num << endl;
}
}
return 0;
}
//埋头写了一上午,100多行代码,最后就过了两个点,而且极其复杂,看了别人的代码瞬间就觉得思维了开阔了好多,
//原本复杂的事情变得简单易懂所以没可行思路的时候不要强上,写出一大篇复杂的代码,最后把自己弄晕,有这时间
//不妨去看看别人的思路
//这题要注意为13的整数倍时不需要输出各位。