題目描述:
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
題目思路:
進行十三進制和十進制的互相轉換,十進制的正常意義上的,十三進制是題目重新規定的,給出了高位和低位的表示方法。
1.13的倍數可以直接用1個高位“火星文”表示,不需要在低位補“tret”即"0"。
2.當輸入的“火星文”隻有一個單詞的時候,需要判斷是高位還是低位。
題目代碼:
#include <cstdio>
#include <string>
#include <map>
#include <iostream>
using namespace std;
map<string, int>m1; // 低位
map<string, int>m2; // 高位
map<string, int>m3; // 低位+高位
map<int, string>n1; // 低位
map<int, string>n2; // 高位
map<int, string>n3; // 低位+高位
int num1[13] = {0,1,2,3,4,5,6,7,8,9,10,11,12};
int num2[26] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25};
string str1[13] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
string str2[13] = {"tret","tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
string str3[26] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec",
"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
string s1;
int n;
int main(){
scanf("%d",&n);
// 初始化 高 低位
for(int i = 0; i < 13; i++){
m1[str1[i]] = num1[i];
m2[str2[i]] = num1[i];
n1[num1[i]] = str1[i];
n2[num1[i]] = str2[i];
}
// 初始化 全
for(int i = 0; i < 26; i++){
m3[str3[i]] = num2[i];
n3[num2[i]] = str3[i];
}
getchar();
for(int i = 0; i < n; i++){
getline(cin,s1);
//數字
if(s1[0] >= '0' && s1[0] <= '9'){
int temp;
sscanf(s1.c_str(),"%d",&temp);
if(temp < 13)
cout<<n1[temp]<<endl;
else if(temp % 13 == 0)
cout<<n2[temp/13]<<endl;
else
cout<<n2[temp/13]<<" "<<n1[temp%13]<<endl;
}
// 字元串
else{
if(s1.length() == 3)
if(m3[s1] >= 13)
cout<<(m3[s1]-12) * 13<<endl;
else
cout<<m3[s1]<<endl;
else if(s1.length() == 4)
cout<<0<<endl;
else{
cout<<(m2[s1.substr(0,3)])*13+m1[s1.substr(4,3)]<<endl;
}
}
}
return 0;
}