PAT Basic 1044. 火星数字
PAT Basic 1044. 火星数字
1. 题目描述:
火星人是以 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
。为了方便交流,请你编写程序实现地球和火星数字之间的互译。
2. 输入格式:
输入第一行给出一个正整数 \(N\)(\(<100\)),随后 \(N\) 行,每行给出一个 [0, 169) 区间内的数字 —— 或者是地球文,或者是火星文。
3. 输出格式:
对应输入的每一行,在一行中输出翻译后的另一种语言的数字。
4. 输入样例:
4
29
5
elo nov
tam
5. 输出样例:
hel mar
may
115
13
6. 性能要求:
Code Size Limit
16 KB
Time Limit
400 ms
Memory Limit
64 MB
思路:
也算考察基础IO,但是这道题卡了好久。。。
首先输入是地球文和火星文随机出现的,需要处理下,这里我开始的想法是都当作数值读入,利用scanf()
返回值来判断是否成功读入,失败时再当作字符串读入。然后就是每行火星文的处理,可能包括一个或两个单词,最开始我还是打算利用scanf()
逐个处理,但是报bug,应该还要额外处理下换行符。后面就是用fgets()
直接读入一行,参见注释部分的代码,根据读入字符串的长度来判断是一个还是两个单词,然后还要额外处理换行符用于strcmp()
比较,写的挺麻烦的。。。
最后的解法使用了strstr()
库函数,相对来说比较简洁了,思路参见的大佬的代码:PAT-Basic-1044. 火星数字 – Lnyan's Blog (llonely.com)
这里一开始testpoint2,4报答案错误,有个bug点是地球数字转火星文时,只存在高位的话,低位是不用输出tret的,比如26 == hel,!= hel tret。
My Code:
// #include <stdio.h>
// #include <string.h> // strcmp header
// #define MARS_BASE 13
// int main(void)
// {
// char * low[] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly",
// "aug", "sep", "oct", "nov", "dec"}; //13 elements
// char * high[] = {"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo",
// "syy", "lok", "mer", "jou"}; // 12 elements
// int digitNum = 0;
// int i=0;
// int tempInt = 0;
// char tempChar[10] = "";
// int j=0;
// scanf("%d", &digitNum);
// for(i=0; i<digitNum; i++)
// {
// if(scanf("%d", &tempInt)) // input is from earth
// {
// if(tempInt/MARS_BASE)
// {
// if(tempInt%MARS_BASE)
// {
// printf("%s %s\n", high[tempInt/MARS_BASE - 1], low[tempInt%MARS_BASE]);
// }
// else //first submit testpoint2,4 get wrong answer for 26 == hel, != hel tret
// {
// printf("%s\n", high[tempInt/MARS_BASE - 1]);
// }
// }
// else
// {
// printf("%s\n", low[tempInt]);
// }
// }
// else // input is from mars
// {
// tempInt = 0; // to store result
// fgets(tempChar, 10, stdin);
// if(strlen(tempChar)>4) // both have high order and lower order
// {
// tempChar[3] = '\0'; // high order must only have 3 letters
// //printf("Test: %s\n", tempChar);
// for(j=0; j<12; j++) // search high order
// {
// if(!strcmp(tempChar, high[j]))
// {
// tempInt += (j+1)*MARS_BASE;
// break;
// }
// }
// *(strchr(tempChar+4, '\n')) = '\0'; // eleminate the return character
// //printf("Test: %s\n", tempChar+4);
// for(j=0; j<13; j++) // search low order
// {
// if(!strcmp(tempChar+4, low[j]))
// {
// tempInt += j;
// break;
// }
// }
// }
// else // have high order or lower order
// {
// *(strchr(tempChar, '\n')) = '\0'; // eleminate the return character
// //tempChar[3] = '\0'; // eliminate return character
// //printf("Test: %s\n", tempChar);
// for(j=0; j<12; j++) // search high order
// {
// if(!strcmp(tempChar, high[j]))
// {
// tempInt += (j+1)*MARS_BASE;
// break;
// }
// }
// for(j=0; j<13; j++) // search low order
// {
// if(!strcmp(tempChar, low[j]))
// {
// tempInt += j;
// break;
// }
// }
// }
// // while(scanf("%s",tempChar))
// // {
// // for(j=0; j<12; j++) // search high order
// // {
// // if(!strcmp(tempChar, high[j]))
// // {
// // tempInt += (j+1)*MARS_BASE;
// // break;
// // }
// // }
// // for(j=0; j<13; j++) // search low order
// // {
// // if(!strcmp(tempChar, low[j]))
// // {
// // tempInt += j;
// // break;
// // }
// // }
// // }
// printf("%d\n", tempInt);
// }
// }
// return 0;
// }
#include <stdio.h>
#include <string.h> // strcmp header
#define MARS_BASE 13
int main(void)
{
char * low[] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly",
"aug", "sep", "oct", "nov", "dec"}; //13 elements
char * high[] = {"Null", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo",
"syy", "lok", "mer", "jou"}; // 13 elements
int digitNum = 0;
int i=0;
int tempInt = 0;
char tempChar[10] = "";
int j=0;
scanf("%d", &digitNum);
for(i=0; i<digitNum; i++)
{
if(scanf("%d", &tempInt)) // input is from earth
{
if(tempInt/MARS_BASE)
{
if(tempInt%MARS_BASE)
{
printf("%s %s\n", high[tempInt/MARS_BASE], low[tempInt%MARS_BASE]);
}
else //first submit testpoint2,4 get wrong answer for 26 == hel, != hel tret
{
printf("%s\n", high[tempInt/MARS_BASE]);
}
}
else
{
printf("%s\n", low[tempInt]);
}
}
else // input is from mars
{
tempInt = 0; // to store result
fgets(tempChar, 10, stdin);
for(j=0; j<13; j++)
{
if(strstr(tempChar, high[j]))
{
tempInt += j*MARS_BASE;
break;
}
}
for(j=0; j<13; j++)
{
if(strstr(tempChar, low[j]))
{
tempInt += j;
break;
}
}
printf("%d\n", tempInt);
}
}
return 0;
}