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进制的计数法,我们要把地球上的数字转化为火星上的数字,或者相反。
思路:
这种题就是坑人的,明明是很简单的思路,却要方方面面都考虑到,首先就是如果地球数字是“13”那么火星数字是“tam”而不是“tam tret”,就是如果地球数字是13的倍数,火星数字后面是不带“tert”的。
代码:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
int n,i,j;
string mars1[]={"tret","jan", "feb", "mar",
"apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string mars2[]={" ","tam", "hel", "maa", "huh", "tou", "kes",
"hei", "elo", "syy", "lok", "mer", "jou"};
scanf("%d",&n);
getchar();
while(n--) {
string t;
int temp;
getline(cin,t);
if(t[0]>='0'&&t[0]<='9') {
temp=stoi(t);
vector<int> mm;
if(temp==0) {
printf("tret\n");
continue;
}
while(temp) {
int a=temp%13;
temp/=13;
mm.push_back(a);
}
if(mm.size()==2) {
if(mm[0]==0)
cout<<mars2[mm[1]]<<endl;
else
cout<<mars2[mm[1]]<<" "<<mars1[mm[0]]<<endl;
}
else if(mm.size()==1) {
cout<<mars1[mm[0]]<<endl;
}
}
else {
if(t.size()>4) {
string a=t.substr(0,3);
string b=t.substr(4,t.size());
for(i=1;i<13;i++) {
if(a==mars2[i])
break;
}
int a1=i;
for(i=0;i<13;i++) {
if(b==mars1[i])
break;
}
int b1=i;
printf("%d\n",a1*13+b1);
}
else {
for(i=0;i<13;i++) {
if(t==mars1[i]) {
printf("%d\n",i);
break;
}
}
if(i==13) {
for(i=1;i<13;i++) {
if(t==mars2[i]) {
printf("%d\n",i*13);
break;
}
}
}
}
}
}
return 0;
}