People on Mars count their numbers with base 13:
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.
Each input file contains one test case. For each case, the first line contains a positive integer N (<). Then N lines follow, each contains a number in [0, 169), given either in the form of an Earth number, or that of Mars.
For each number, print in a line the corresponding number in the other language.
4
29
5
elo nov
tam
hel mar may 115 13
1 #include <bits/stdc++.h> 2 using namespace std; 3 map<string,int>mp1; 4 map<string,int>mp2; 5 string s; 6 int l; 7 //mo 为余数,mar 为商 8 string mo[15]={"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"}; 9 string mar[15]={"","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"}; 10 void init() 11 { 12 for(int i =0;i<=12;i++){ 13 mp1[mar[i]] = i; 14 } 15 for(int i =1;i<=12;i++){ 16 mp2[mo[i]] = i; 17 } 18 } 19 void zifu(string s) 20 { 21 int ans = 0; 22 for(int i =0;s[i];i++){ 23 ans =ans*10+s[i]-‘0‘; 24 } 25 int x=ans/13;int y= ans%13; 26 if(x==0){ 27 cout<<mo[y]<<endl; 28 } 29 else{ 30 if(y==0){ 31 cout<<mar[x]<<endl;//如 : 13 :tam 后面没有tret 32 } 33 else{ 34 cout<<mar[x]<<" "<<mo[y]<<endl; 35 } 36 } 37 } 38 void isnum(string s){ 39 string s1,s2; 40 int p=0,q=0; 41 if(l<=3){//可能地球文字也可能火星文字 42 p=mp1[s]; 43 q=mp2[s]; 44 } 45 else{ 46 s1 =s.substr(0,3);//从0开始数3个 47 s2 =s.substr(4,3); 48 p =mp1[s1]; 49 q =mp2[s2]; 50 } 51 printf("%d\n",p*13+q); 52 } 53 int main() 54 { 55 56 57 int n; 58 scanf("%d",&n); 59 getchar(); 60 while(n--){ 61 init(); 62 getline(cin,s); 63 l =s.length(); 64 if(isdigit(s[0])){ 65 zifu(s); 66 } 67 else{ 68 isnum(s); 69 } 70 } 71 return 0; 72 }
原文:https://www.cnblogs.com/tingtin/p/11545048.html