1100 Mars Numbers (20 分)
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
AC代码:
#include<bits/stdc++.h>
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"};
void func1(string &temp){
int temp2=stoi(temp);//将数字字符串转化为数字
if(temp2/13){
cout<<b[temp2/13];
if(temp2%13){
cout<<" "; //由样例可知,如果十位可以表示的话不用输出各位
cout<<a[temp2%13];
}
}
else cout<<a[temp2%13];
}
void func2(string &temp){
int len=temp.length();
int ge=0,shi=0;
string gem=temp.substr(0,3),shim;
if(len>4){
shim=temp.substr(4,3);
gem=temp.substr(0,3);
}
for(int i=1;i<=12;i++){
if(a[i]==gem||a[i]==shim)ge=i;
if(b[i]==gem)shi=i;//前面的可能是十位也可能是百位
}
printf("%d",shi*13+ge);
}
int main()
{
int n;
string temp;
cin>>n;
getchar();//cin会将回车放入缓冲区中,需要读掉
for(int i=0;i<n;i++){
getline(cin,temp);
if(temp[0]>='0'&&temp[0]<='9')
func1(temp);
else
func2(temp);
cout<<"\n";
}
}