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 (<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 • 1 • 2 • 3 • 4 • 5
Sample Output:
hel mar may 115 13
题意
火星人用 13 进制来计数:
zero(零)在火星读作 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。
我们要做的就是转换火星数字与正常数字。
思路
处理正常数字
如果数字小于 13 ,则直接查找数组输出对应的值。
如果数字大于等于 13 ,则需要将数字划分成高低两位,这里需要注意的是,如果该数能被 13 除尽的话是只输出一个字符串的,例如 13 输出 tam 而不是 tam tret ,当有高位存在时低位如果为 0 则不进行输出。当然如果数字等于 0 的话,直接输出 tret 即可。
处理火星数字
如果给定的数字在高位数组中,则需要将数字转换成对应的正常数字并乘以 13 返回。
如果给定的数字在低位数组中,则直接查询并返回对应的数值即可。
代码
#include<bits/stdc++.h> using namespace std; string high[13] = { "","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou" }; string low[13] = { "tret","jan", "feb","mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec" }; int get(string str) { //判断是否在高位 for (int i = 1; i <= 12; i++) if (str == high[i]) return i * 13; //判断是否在低位 for (int i = 0; i <= 12; i++) if (str == low[i]) return i; } void change(string num) { if (num[0] >= '0' && num[0] <= '9') //处理正常数字 { int ans = stoi(num); if (ans < 13) cout << low[ans] << endl; else { cout << high[ans / 13]; if (ans % 13) cout << " " << low[ans % 13]; cout << endl; } } else //处理火星数字 { stringstream ssin(num); string str; int ans = 0; while (ssin >> str) ans += get(str); cout << ans << endl; } } int main() { int n; cin >> n; getchar(); for (int i = 0; i < n; i++) { string num; getline(cin, num); change(num); } return 0; }