1044. 火星数字(20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
火星人是以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,怎样输入,因为每行的输入数目不定,所以这里没有使用scanf("%s", &str)输入,而是参用另一种常用的模式gets(str),然后再把str中的字符解析到自己设计的数据结构中;
2,本题情况较多,所以要考虑周全;
3,定一个可行的方案,仔细考虑到方案的每一步,别等到写完了发现方案有漏洞.
代码:
/*************************************************************************> File Name: 1044.c> Author: YueBo> Mail: yuebowhu@163.com> Created Time: Sat 10 Jun 2017 11:49:01 AM CST************************************************************************/#include <stdio.h>
#include <stdlib.h>
#include <string.h>char *digit[13] = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
char *decimal_2[13] = {"", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};int position(char *str, char **pstr, int len)
{int i = 0;for (i = 0; i < len; i++){if (!strcmp(str, *(pstr+i)))return i;}return -1;
}int main()
{int N, i, j, k;char str_tmp[16], str_dcm[8], str_dgt[8];int val, dgt, dcm, dcm1;int ch;scanf("%d", &N);ch = getchar();for (i = 0; i < N; i++){gets(str_tmp);k = 0;while (str_tmp[k] == ' ')k++;j = 0;while (str_tmp[k] != ' ' && str_tmp[k] != '\0'){str_dcm[j] = str_tmp[k];j++;k++;}str_dcm[j] = '\0';while (str_tmp[k] == ' ')k++;j = 0;while (str_tmp[k] != ' ' && str_tmp[k] != '\0'){str_dgt[j] = str_tmp[k];j++;k++;}str_dgt[j] = '\0';dcm = position(str_dcm, decimal_2, 13);dcm1 = position(str_dcm, digit, 13);dgt = position(str_dgt, digit, 13);if (dcm != -1 && dgt != -1)val = dcm * 13 + dgt;else if (dcm != -1)val = dcm * 13;else if (dcm1 != -1)val = dcm1;else{val = atoi(str_dcm);dgt = val % 13;dcm = (val / 13) % 13;printf("%s%s%s\n", decimal_2[dcm], dgt!=0&&dcm!=0 ? " ":"", dgt==0 && dcm!=0 ? "":digit[dgt]);continue;}printf("%d\n", val);}return 0;
}