1100 Mars Numbers
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 (<). 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
分析:
本来以为这是一道简单题,但是中间有好多细节如果注意不到的话,真的很无语。
1. main函数中cin >> n;输入之后要用getchar();来接受输入的回车键,这个地方卡了我很长时间。
2. 数字13按照正常的逻辑应该是“tam tret",但是样例中只给出了”tam“这一个字母,说明后面的0应该省略不写。这种细节要是我在考试中肯定不会发现。
3. 最后还是有两组数据没有通过。就差把别人的代码给粘贴上来了,但是就是找不到为什么出错。
Code:
#include<iostream> #include<string> using namespace std; string lowDigit[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"}; string highDigit[13] = {"###", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"}; void MarToEarth(string str) { int len = str.length(); int t1 = 0, t2 = 0; string s1 = str.substr(0, 3), s2; if (len > 4) s2 = str.substr(4, 3); for (int i = 1; i < 13; ++i) { if (lowDigit[i] == s1 || lowDigit[i] == s2) t2 = i; if (highDigit[i] == s1) t1 = i; } cout << t1*13 + t2 << endl; } void EarthToMar(string str) { int n = stoi(str); int low = n % 13; int high = n / 13; if (high) cout << highDigit[high]; if ((high) && (low)) cout << " "; if (low || n == 0) cout << lowDigit[low] << endl; } int main() { int n; cin >> n; getchar(); string num; for (int i = 0; i < n; ++i) { getline(cin, num); if (num[0] >= '0' && num[0] <= '9') EarthToMar(num); else MarToEarth(num); } return 0; }