题目
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
Sample Output:
hel mar
may
115
13
题意
将地球的十进制数按照火星的十三进制规则进行互译。
解答
火星文为字符串,使用map容器进行存储对数字的映射,数字转火星文可以直接使用数组,为加强map的使用这里也建立了两个map容易来实现变换。
代码如下:
#include <map>
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
int str2int(string str){
int i,ans = 0;
for(i=0;i<str.size();i++){
ans*=10;
ans = ans + str[i] - '0';
}
return ans;
}
int main(){
int i,n,j,num;
string fdigit[15] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string sdigit[15] = {"tret","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
string str;
map<string,int> mp;
map<int,string> mp1;
map<int,string> mp2;
for(i = 0;i<13;i++){
mp1[i] = fdigit[i];
mp2[i] = sdigit[i];
mp[fdigit[i]] = i;
mp[sdigit[i]] = i*13;
}
for(i = 0;i<13;i++)
for(j = 0;j<13;j++){
str = sdigit[i] + " " + fdigit[j];
mp[str] = i*13+j;
}
scanf("%d",&n);
getchar();
for(i=0;i<n;i++){
getline(cin,str);
if(str[0] >='0' && str[0] <= '9'){
num = str2int(str);
if(num < 13){
printf("%s\n",fdigit[num].c_str());
}
else if(num%13 == 0){
printf("%s\n",sdigit[num/13].c_str());
}
else{
printf("%s %s\n",sdigit[num/13].c_str(),fdigit[num%13].c_str());
}
}
else{
printf("%d\n",mp[str]);
}
}
return 0;
}