火星人是以 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
-
思路 1:
分别用 :char数组存储:数字->火星文(str),map存储:火星文(str)->数字
输入时先判断是数字还是字母
1)对于mars->e: 有两种情况(通过字符串长度判断)- 一位火星文 :直接通过map输出对应的数字
- 两位火星文:输出各位火星文对应数字的和(存入map时已将高位火星文*13)
2)对于e->mars: 也有两种情况 - 比12小(包含12):只输出一位火星文
- 比12大:如果刚好能整除13(num%13 == 0)只输出高位火星文,否则先输出高位火星文(num/13),再输出低位火星文(num%13)
-
code 1:
#include <iostream>
#include <algorithm>
#include <string>
#include <unordered_map>
#include <ctype.h>
using namespace std;
unordered_map<string, int> mars_e;
char fdig[15][5] = {"tret", "jan", "feb", "mar", "apr", "may",
"jun", "jly", "aug", "sep", "oct", "nov", "dec"};
char ndig[15][5] = {"tret", "tam", "hel", "maa", "huh", "tou",
"kes", "hei", "elo", "syy", "lok", "mer", "jou"};
int main(){
int n;
scanf("%d", &n);
getchar();
for(int i = 0; i < 13; ++i){
mars_e[fdig[i]] = i;
mars_e[ndig[i]] = i*13;
}
for(int i = 0; i < n; ++i){
string s;
getline(cin, s);
if(isalpha(s[0])){
//mars->e
if(s.size() == 3)
cout << mars_e[s]<<endl;
else{
string s1 = s.substr(0, 3);
string s2 = s.substr(4, 3);
cout << mars_e[s1]+mars_e[s2]<<endl;
}
}else{
//e-mars
int ans = stoi(s);
if(ans > 12){
cout << ndig[ans/13];
if(ans % 13 != 0)
cout << " " << fdig[ans%13]<<endl;
else
cout << endl;
}else{
cout << fdig[ans]<<endl;
}
}
}
return 0;
}
- T2 code:
#include <bits/stdc++.h>
using namespace std;
string d[2][13] = {
{"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"},
{"tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"}};
unordered_map<string, int> mp;
void Earth_Mars(int x){
if(x % 13 == 0){
printf("%s\n", d[1][x / 13].c_str()); //Wrong 2、4
}else if(x > 12){
printf("%s %s\n", d[1][x / 13].c_str(), d[0][x % 13].c_str());
}else{
printf("%s\n", d[0][x].c_str());
}
}
void Mars_Earth(string s){
if(s.size() == 3) printf("%d\n", mp[s]);
else{
string sub1 = s.substr(0, 3), sub2 = s.substr(4, 3);
int x = mp[sub1] + mp[sub2];
printf("%d\n", x);
}
}
int main(){
int n;
scanf("%d", &n);
getchar();
for(int i = 0; i < 13; ++i){
mp[d[0][i]] = i;
mp[d[1][i]] = 13 * i;
}
for(int i = 0; i < n; ++i){
string s;
getline(cin, s);
if(isalpha(s[0])){
Mars_Earth(s);
}else{
Earth_Mars(stoi(s));
}
}
return 0;
}