题意概述:
火星人是以 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 <stdio.h>
#include <math.h>
#include <string.h>
int earth_to_mar(char *p)
{
char word1[13][5]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};
char word2[13][5]={"tret","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
int k,length,number=0;
for (length=0;p[length]!='\0';length++);
for (k=length-1;k>=0;k--)
number+=pow(10,length-(k+1))*(p[k]-'0');
if (number<=12)
printf("%s",word1[number]);
else
{
printf("%s",word2[number/13]);
if (number%13!=0)
printf(" %s",word1[number%13]);//这个空格加在哪儿很关键
}
return 0;
}
int mar_to_earth(char *q)
{
char word1[13][5]={"tret","jan","feb","mar","apr","may","jun","jly","aug","sep","oct","nov","dec"};//这里tret字符长度4,再加一个\n
char word2[13][5]={"tret","tam","hel","maa","huh","tou","kes","hei","elo","syy","lok","mer","jou"};
int k,length,number=0;
for (length=0;q[length]!='\0';length++);
if (length==3||length==4)
{
for (k=0;k<13;k++)
if (q[0]==word1[k][0]&&q[1]==word1[k][1]&&q[2]==word1[k][2])
{
printf("%d",k);break;
}
for (k=1;k<13;k++)//注意3个字符,但是只有十位,没有个位,把tret情况排除
if (q[0]==word2[k][0]&&q[1]==word2[k][1]&&q[2]==word2[k][2])
{
printf("%d",13*k);
}
}
else
{
for (k=0;k<13;k++)
if (q[0]==word2[k][0]&&q[1]==word2[k][1]&&q[2]==word2[k][2])
{
number+=k*13;break;
}
for (k=0;k<13;k++)
if (q[4]==word1[k][0]&&q[5]==word1[k][1]&&q[6]==word1[k][2])
{
number+=k;break;
}
printf("%d",number);
}
return 0;
}
int main()
{
char num[8]={'\0'};
int n=0,i,j;
scanf("%d\n",&n);//因为后面都是用gets,所以加\n
for (i=0;i<n;i++)
{
gets(num);
if (num[0]>='0'&&num[0]<='9')
earth_to_mar(num);
else if(num[0]>='a'&&num[0]<='z')
mar_to_earth(num);
if (i!=n-1)
printf("\n");
for (j=0;num[j]!='\0';j++)
num[j]='\0';
}
return 0;
}
Tips:
1.注意火星文,进位后无需地位有0;
2.字符较多,注意输入的正确性,别多打空格
提交结果: