火星人是以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
Answer1:
#include<iostream>#include<string>#define is_digit(c) (c)>='0' && (c)<='9'using namespace std;static string ones[13]= {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};static string tens[13]= {"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};void print_earth_number(string& s) { int num = 0; for(int k = 0; k < s.size(); k += 4) { string temp = s.substr(k, 3); for(int j = 0; j < 13; j ++) if(temp == tens[j]) num += (j+1)*13; else if(temp == ones[j]) num += j; } cout << num << '/n';}void print_mars_number(int t) { if(t < 13) cout << ones[t%13] << '/n'; else if(t >= 13 && t%13 == 0) cout << tens[t/13-1] << '/n'; else if(t > 13) cout << tens[t/13-1] << ' ' << ones[t%13] << '/n';}int main() {int n = 0; cin >> n; string in[n]; cin.get(); for(int i = 0; i < n; i++) { getline(cin, in[i]); } for(int i = 0; i < n; i ++) { if(is_digit(in[i][0])) { int t = 0, j = 0; while(in[i][j]) t = t*10 + in[i][j++] - '0'; print_mars_number(t); } else { print_earth_number(in[i]); } }}PS. 这个是正常的解法。 我本来想了一种不正常的解法,一会儿写在下面。 为什么会从不正常的写法换成正常的写法呢。因为一直通不过测试。 但是通不过测试并不是因为不正常的写法有问题,而是因为在我的机器上可以用cin.sync()但并不能通过测试(好像不是第一次遇到这个问题了T^T)。换成cin.get()就可以了。 不正常的解法:
新闻热点
疑难解答