1044 火星數字 (20 分)
火星人是以 13 進制計數的:
- 地球人的 0 被火星人稱爲 tret。
- 地球人數字 1 到 12 的火星文分別爲:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。
- 火星人將進位以後的 12 個高位數字分別稱爲:tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou。
例如地球人的數字 29
翻譯成火星文就是 hel mar
;而火星文 elo nov
對應地球數字 115
。爲了方便交流,請你編寫程序實現地球和火星數字之間的互譯。
輸入格式:
輸入第一行給出一個正整數 N(<100),隨後 N 行,每行給出一個 [0, 169) 區間內的數字 —— 或者是地球文,或者是火星文。
輸出格式:
對應輸入的每一行,在一行中輸出翻譯後的另一種語言的數字。
輸入樣例:
4
29
5
elo nov
tam
輸出樣例:
hel mar
may
115
13
#include<string>
#include<cctype>
#include<iostream>
using namespace std;
string a[14]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};//低位
string b[14]={"tret","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};//高位
int earth(string s0){//處理火星文
int x=0;
if(s0=="tret")
return x;//返回0
int k=0;
if(s0[3]==' ')
k=4;
for(int i=1;i<=12;i++){
if(s0[0+k] == a[i][0] && s0[1+k] == a[i][1] && s0[2+k] == a[i][2])
x+=i;//算出低位
if(s0[0] == b[i][0] && s0[1] == b[i][1] && s0[2] == b[i][2])
x+=i*13;//算出高位
}
return x;//返回
}
void mars(int t){//處理數字
if(t==0)
cout<<a[t]<<endl;//直接輸出0對應的字符
else {
int t1=t/13;//求出高位數字
int t2=t%13;//求出尾數
if(t1!=0){
cout<<b[t1];
if(t2!=0)
cout<<' ';
}//輸出高位數字對應字符
if(t2!=0)
cout<<a[t2];
cout<<'\n';
}//輸出尾數對應字符
}
int main(){
int n;
cin>>n;//輸入有幾個數字
getchar();//從緩衝區讀走一個字符,相當於清除緩衝區
for(int i=0;i<n;i++){
string s;
getline(cin,s);//輸入表示遇到空格(\n)終止
if(s[0]>='0'&&s[0]<='9'){
int earth1=0;
for(int k=0;k<s.length();k++)
earth1=earth1*10+(s[k]-'0');
mars(earth1);
}
else{
int mars1;
mars1=earth(s);
cout<<mars1<<endl;
}
}
}