题解 | #学英语#
学英语
http://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
我就看看有谁的代码比我的长
while True:
try:
num = list(input().rjust(7,'0'))
#构造个位、十位、百位词典
dic0 = {'1':'one','2':'two','3':'three','4':'four', '5':'five', '6':'six', '7':'seven', '8':'eight', '9':'nine'}
dic1 = {'2':'twenty','3':'thirty','4':'forty', '5':'fifty', '6':'sixty', '7':'seventy', '8':'eighty', '9':'ninety'}
dic2 = {'0':'ten', '1':'eleven', '2':'twelve','3':'thirteen','4':'fourteen', '5':'fifteen', '6':'sixteen','7':'seventeen', '8':'eighteen', '9':'nineteen'}
output = []
#右起每3位一组,方便处理和最后链接
mill = num[0:1]
thou = num[1:4]
base = num[4:8]
#开关,用于判断十位为1时个位数字是否还单独输出
k = True
#判断是否加million
if mill[0] != '0':
output.append(dic0[mill[0]] + ' '+ 'million' + ' ')
#中间3位
if thou[0] != '0':
if thou[1] == '0' and thou[2] == '0':
output.append(dic0[thou[0]] + ' '+ 'hundred' + ' ')
else:
output.append(dic0[thou[0]] + ' '+ 'hundred' + ' ' + 'and' + ' ')
if thou[1] != '0' and thou[1] != '1':
output.append(dic1[thou[1]] + ' ')
#当十位数字为1时,将开关k置为False,屏蔽个位数字输出
if thou[1] == '1':
output.append(dic2[thou[2]] + ' ')
k = False
if thou[2] != '0' and k :
output.append(dic0[thou[2]] + ' ')
#判断是否加thousand
if thou[0] == '0' and thou[1] == '0' and thou[2] == '0':
pass
else:
output.append('thousand' + ' ')
#后3位,并将k重置
k = True
if base[0] != '0':
if base[1] == '0' and base[2] == '0':
output.append(dic0[base[0]] + ' '+ 'hundred' + ' ')
else:
output.append(dic0[base[0]] + ' '+ 'hundred' + ' ' + 'and'+ ' ')
if base[1] != '0' and base[1] != '1':
output.append(dic1[base[1]] + ' ')
if base[1] == '1':
output.append(dic2[base[2]] + ' ')
k = False
if base[2] != '0' and k :
output.append(dic0[base[2]] + ' ')
else:
pass
#列表转字符串
final_output = ''
for i in output:
final_output += i
print(final_output)
except:
break