PAT乙级1044
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;
}
}
}
还没有评论,来说两句吧...