火星人是以 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) 区间内的数字 —— 或者是地球文,或者是火星文。
输出格式:
对应输入的每一行,在一行中输出翻译后的另一种语言的数字。
如果第一位是数字,就按数字转成火星,如果不是数字,就查找对比字符串,然后转换成数字就行了
主要在于判断数字还是火星文字上
#include
#include
int main()
{
int N=0;
char low[13][5]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
char hight[13][4]={"*","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
char temp[8];
scanf("%d\n",&N);
for(int i=0;i<N;i++)
{
gets(temp);
if(temp[0]>='0'&&temp[0]<='9')//地球转火星
{
int count=0,len=0;
len=strlen(temp);
for(int i=0;i<len;i++)//把字符转化为数字
{
count=count*10+temp[i]-'0';
}
if(count>=13)//大于13有高位
{
printf("%s",hight[count/13]);//输出高位
if(count%13!=0)//如果取余不为0,有低位
{
printf(" %s",low[count%13]);//输出低位
}
}
else
{
printf("%s",low[count%13]);//只有低位输出即可
}
}
else//火星转地球
{
int count2=0;
for(int i=0;i<13;i++)
{
if(strstr(temp,low[i]))
{
count2=count2+i;//如果在低位的二维字符串数组找到
break;
}
}
for(int i=1;i<13;i++)
{
if(strstr(temp,hight[i]))
{
count2=count2+i*13;//如果在高位的二维字符串数组找到要乘13
break;
}
}
printf("%d",count2);
}
printf("\n");
}
}