People on Mars count their numbers with base 13:
Zero on Earth is called “tret” on Mars.
The numbers 1 to 12 on Earch 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
Sample Output:
hel mar
may
115
13
题目大意:火星人是以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”。为了方便交流,请你编写程序实现地球和火星数字之间的互译~
分析:因为给出的可能是数字(地球文)也有可能是字母(火星文),所以用字符串s保存每一次的输入,因为如果是火星文则会出现空格,所以用getline接收一行的输入~计算string s的长度len,判断s[0]是否是数字,如果是数字,表示是地球文,则需要转为火星文,执行func1();如果不是数字,则说明是火星文,需要转为地球文,执行func2();
func1(int t)中,传入的值是string转int后的结果stoi(s),因为数字最大不超过168,所以最多只会输出两位火星文,如果t / 13不等于0,说明有高位,所以输出b[t/13];如果输出了高位(t/13不等于0)并且t % 13不等于0,说明有高位且有低位,所以此时输出空格;如果t % 13不等于0,说明有低位,此时输出a[t % 13];注意,还有个数字0没有考虑,因为数字0取余13等于0,但是要特别输出tret,所以在func1的最后一句判断中加一句t == 0,并将a[0]位赋值成tret即可解决0的问题~
func2()中,t1和t2一开始都赋值0,s1和s2用来分离火星文单词,因为火星文字符串只可能一个单词或者两个单词,而且一个单词不会超过4,所以先将一个单词的赋值给s1,即s1 = s.substr(0, 3);如果len > 4,就将剩下的一个单词赋值给s2,即s2 = s.substr(4, 3);然后下标j从1到12遍历a和b两个数组,如果a数组中有和s1或者s2相等的,说明低位等于j,则将j赋值给t2;如果b数组中有和s1相等的(b数组不会和s2相等,因为如果有两个单词,s2只可能是低位),说明高位有值,将j赋值给t1,最后输出t1 * 13 + t2即可~
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 |
#include <iostream> #include <string> using namespace std; string a[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"}; string b[13] = {"####", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"}; string s; int len; void func1(int t) { if (t / 13) cout << b[t / 13]; if ((t / 13) && (t % 13)) cout << " "; if (t % 13 || t == 0) cout << a[t % 13]; } void func2() { int t1 = 0, t2 = 0; string s1 = s.substr(0, 3), s2; if (len > 4) s2 = s.substr(4, 3); for (int j = 1; j <= 12; j++) { if (s1 == a[j] || s2 == a[j]) t2 = j; if (s1 == b[j]) t1 = j; } cout << t1 * 13 + t2; } int main() { int n; cin >> n; getchar(); for (int i = 0; i < n; i++) { getline(cin, s); len = s.length(); if (s[0] >= '0' && s[0] <= '9') func1(stoi(s)); else func2(); cout << endl; } return 0; } |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼