A1100. Mars Numbers (20)
2016-03-11 21:58
288 查看
题目描述
People on Mars count their numbers with base 13:Zero on Earth is called "tret" on Mars.
The numbers 1 to 12 on Earch 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.
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.
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
题意解析
这道题目就是根据题目描述,进行地球数字和火星数字的互换。解题思路
按照题目描述一步一步做就ok,注意点是格式问题;当输入13时,输出是tam,而不是tam tret。参考代码
#include <cstdio> #include <string.h> #include <algorithm> using namespace std; const int maxn = 110; char mars_number[maxn][maxn] = {"tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"}; char mars_number1[maxn][maxn] = {"tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"}; int n; int main() { int l,result=0; char str[maxn]; bool flag = false; scanf("%d",&n); getchar(); for(int i=0;i<n;i++) { flag = false; result = 0; gets(str); l = strlen(str); for(int i=0;i<l;i++) { if(str[i]>='0' && str[i] <='9') { result = result * 10 + str[i] - '0'; flag = true; } } if(flag) { int a,b; a = result / 13; b = result % 13; if(a != 0 && b != 0) printf("%s %s\n",mars_number1[a-1],mars_number[b]); if(a != 0 && b == 0) printf("%s\n",mars_number1[a-1]); if(a == 0) printf("%s\n",mars_number[b]); } else { int j = 0; char curr[maxn]; for(int i=0;i<l;i++) { if(str[i] == ' ') { curr[j] = '\0'; for(int x = 0;x < 13;x++) { if(strcmp(curr,mars_number[x]) == 0) { result = result + x; break; } } for(int x = 0;x < 12;x++) { if(strcmp(curr,mars_number1[x]) == 0) { result = result + (x + 1) * 13; break; } } for(int x = 0;x <= j;x++) { curr[x] = '\0'; j = 0; } } else { curr[j++] = str[i]; } } curr[j] = '\0'; for(int x = 0;x < 13;x++) { if(strcmp(curr,mars_number[x]) == 0) { result = result + x; break; } } for(int x = 0;x < 12;x++) { if(strcmp(curr,mars_number1[x]) == 0) { result = result + (x + 1) * 13; break; } } printf("%d\n",result); } } return 0; }
相关文章推荐
- 书评:《算法之美( Algorithms to Live By )》
- 动易2006序列号破解算法公布
- Ruby实现的矩阵连乘算法
- C#插入法排序算法实例分析
- 超大数据量存储常用数据库分表分库算法总结
- C#数据结构与算法揭秘二
- C#冒泡法排序算法实例分析
- 算法练习之从String.indexOf的模拟实现开始
- C#算法之关于大牛生小牛的问题
- C#实现的算24点游戏算法实例分析
- c语言实现的带通配符匹配算法
- 浅析STL中的常用算法
- 算法之排列算法与组合算法详解
- C++实现一维向量旋转算法
- Ruby实现的合并排序算法
- C#折半插入排序算法实现方法
- 基于C++实现的各种内部排序算法汇总
- C++线性时间的排序算法分析
- C++实现汉诺塔算法经典实例
- PHP实现克鲁斯卡尔算法实例解析