1100 Mars Numbers (20 分)
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
题意:火星人的的数字是13进制的,并且写法和地球不同,地球的0在火星上叫 “tret”,地球的1到13在火星上叫 “jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec” ,高一位的1到13叫 “tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou”。根据这个一一对应的关系,将地球和火星的数字进行相互转换。
解题思路:
1.使用2个列表mars1和mars2来存储火星数字,用一个列表number来存储地球数字。
2.定义函数to_mars(n),用来将地球数字转换成火星数字,首先将输入的n转换为int,然后转成2位的13进制数,判断低位是否为0,是则只输出高位对应的火星数字(如数字13对应的13进制是10,输出结果是tam,而不是tam tret),低位不为0,判断高位是否为0,若高位为0则只输出低位对应的火星数字,否则正常输出对应的火星数字。
3.定义函数to_earch(s),用来将火星数字转换为地球数字,首先将输入的s通过空格分割,判断是几位的火星数字,如果是2位,就找到火星数字对应的地球数字,用(高位*13+低位)来求得结果,如果只有1位火星数字,则看这个火星数字在高位列表当中还是在低位列表当中,如果在高位列表当中则找到对应的索引输出(高位*13),否则直接输出低位。
4.首先获取输入的数据个数,控制循环次数,在循环中,每输入一个数据s,对该数据的第一个字符进行判断,如果是数字,则调用函数to_mars(s),否则调用函数to_earch(s)。
mars1 = ['tret','tam', 'hel', 'maa', 'huh', 'tou', 'kes', 'hei', 'elo', 'syy', 'lok', 'mer', 'jou']
mars2 = ['tret','jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jly', 'aug', 'sep', 'oct', 'nov', 'dec']
number = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
def to_mars(n):
n = int(n)
a = n // 13
b = n % 13
if b == 0:
mars = mars1[a]
else:
if a != 0:
mars = mars1[a] + ' ' + mars2[b]
else:
mars = mars2[b]
return mars
def to_earch(s):
s1 = s.split()
if len(s1) == 2:
a = mars1.index(s1[0])
b = mars2.index(s1[1])
c = str(a*13+b)
else:
if s1[0] in mars1:
a = mars1.index(s1[0])
c = str(a*13)
else:
a = mars2.index(s1[0])
c = str(a)
return c
n = int(input())
for i in range(n):
s = input()
if s[0] in number:
print(to_mars(s))
else:
print(to_earch(s))