1100 Mars Numbers
题目
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.
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:
1 | 4 |
Sample Output:
1 | hel mar |
题意
火星的数字是13进制的,并且他们的数字并不用0~9来表示,而是:
- 当数字是1~12时,用jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec来表示1~12.
- 当数字超过12,有两位时,高位用tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou来表示1~12.
- 0用tret表示.
- 当数字是13,也即火星上的"10"时,只用一个"tam"表示,"20"用"hel"表示,也就是说,当火星数字是10,20,30这种整数时,后面的零不用写.
现在要你设计一个翻译程序,如果输入的是地球整数,那就把它翻译成13进制的火星数字;如果输入是火星数字,就把它翻译成10进制的地球数字.
思路
这道题要考虑的情况相当相当多,既要进制转换,又要字符替换,还要对不同长度的数字进行不同情况的分析.
用火星数字获取火星文字,可以直接使用字符串数组,下标表示火星数字,数组存放着火星对应的文字.
用火星文字获取火星数字,可以使用泛型字典,通过火星文字直接获得对应的火星数字.
而这道题明确告诉我们火星的文字只有高低两位,所以判断的时候就比较方便.
代码
首先我们先实现一个地球数字转换成火星文字的函数:
1 | public static string translateToMars(int EarthNumber) |
再实现一个火星文字转地球数字的函数:
1 | public static int translateToEarth(string MarsNumber) |
1 | public static void Main() |