题目描述
People on Mars count their numbers with base 13:
Zero on Earth is called “tret” on Mars.
The numbers 1 to 12 on Earth is called “jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec” on Mars, respectively.
For the next higher digit, Mars people name the 12 numbers as “tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou”, respectively.
For examples, the number 29 on Earth is called “hel mar” on Mars; and “elo nov” on Mars corresponds to 115 on Earth. In order to help communication between people from these two planets, you are supposed to write a program for mutual translation between Earth and Mars number systems.
翻译:火星人用13进制计数:
地球上的0在火星上被称为“tret”。
地球上的1到12在火星上分别被称为"jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec" 。
对于高一位的数字,火星人分别用"tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou"来表示。
例如:地球上的29在火星上被称为 “hel mar” ,火星上的"elo nov"等于地球上的115。为了帮助来自两个星球的人相互交流,你需要写一个程序来进行地火数字互译系统。
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<100). Then N lines follow, each contains a number in [0, 169), given either in the form of an Earth number, or that of Mars.
翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行包括一个正整数N(<100)。接下来N行,每一行包括一个数字[0,169),既可能是地球上的数字格式,也可能是火星上的。
Output Specification:
For each number, print in a line the corresponding number in the other language.
翻译:对于每个数字,输出一行用另一种语言返回的数字。
Sample Input:
4
29
5
elo nov
tam
Sample Output:
hel mar
may
115
13
解题思路
先判断输入的数字是地球格式还是火星格式,然后分别处理,注意0只有在给出0的时候才输出,如果是13,26等是不需要输出0的。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define INF 99999999
#define bug puts("Hello\n")
char low[][5]={"tret","jan","feb","mar","apr","may",
"jun","jly","aug","sep","oct","nov","dec"};
char high[][5]={"tam","hel","maa","huh","tou",
"kes","hei","elo","syy","lok","mer","jou"};
int main(){
int n;
scanf("%d\n",&n);
char s[10];
for(int i=0;i<n;i++){
gets(s);
int length=strlen(s);
if(s[0]>='0'&&s[0]<='9'){
int a=0;
for(int i=0;i<length;i++){
a=a*10+s[i]-'0';
}
if(a/13){
printf("%s",high[a/13-1]);
if(a%13)printf(" %s",low[a%13]);
printf("\n");
}
else printf("%s\n",low[a]);
}
else{
int l=0,ans=0;
char tmp[5]={};
while(s[l]!=' '&&s[l]!='\0'){
tmp[l]=s[l];
l++;
}
int j;
for(j=0;j<12;j++){
if(strcmp(tmp,high[j])==0){
ans+=13*(j+1);
if(s[l]==' '){
l++;
char tmplow[5]={};
int ccount=0;
while(s[l]!='\0'){
tmplow[ccount++]=s[l];
l++;
}
for(int k=0;k<13;k++){
if(strcmp(tmplow,low[k])==0)ans+=k;
}
}
}
}
if(j==12){
for(int k=0;k<13;k++){
if(strcmp(tmp,low[k])==0)ans+=k;
}
}
printf("%d\n",ans);
}
}
return 0;
}