火星人是以 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
分析:写两个函数,一个数字转火星文,一个火星文转数字。
数字转火星文:将字符串变成数字再处理
火星文转数字:分三种情况。1.只有一个火星文 2.有两个火星文 3.是“tret"
代码:
#include <iostream>
#include <string>
using namespace std;
void fun1(string t);
void fun2(string t);
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"};
int tt,temp,temp1,ans = 0;
int main(){
string t;
int n;
cin>>n;
getchar();
for(int i = 0;i<n;i++){
getline(cin,t);
if(t[0]>='0'&&t[0]<='9')fun1(t);//数字转火星文
else fun2(t);//火星文转数字
cout<<endl;
}
return 0;
}
void fun1(string t){
int len = t.length();
int num = 0;
for(int i = 0;i<len;i++)//将字符串变成数字
{
num = num*10+(t[i]-'0');
}
cout<<b[num/13];
if((num%13)&&(num/13))cout<<" "<<a[num%13];//如果有进位和低位
else if(num%13)cout<<a[num%13];//没有进位
else if(num==0)cout<<"tret";//为零的情况
}
void fun2(string t){
if(t.size()==3)//只有一个火星文
{
for(int i = 0;i<13;i++){//遍历高位
if(t==b[i]){
cout<<i*13;
break;
}
}
for(int i = 0;i<13;i++){//遍历低位
if(t==a[i]){
cout<<i;
}
}
}
else if(t.size()==4)cout<<"0";//有四位肯定是零
else //有两个火星文
{
string aa = t.substr(0,3);//高位火星文
string bb = t.substr(4,3);//低位火星文
for(int i = 0;i<13;i++){
if(aa==b[i])temp = i*13;
if(bb==a[i])temp1 = i;
}
cout<<temp+temp1;
}
}