题解 | #学英语#
学英语
http://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
合着英语是这么学的。。。
应该用列表的,但写到一半不想改了。。。
正常逻辑就是每次对10取模,然后根据count来判断现在是个位,十位还是百位。count=3时是百位,但这会有个问题就是输入100会输出one hundred and,所以最后一步判断最后是不是多一个and,是的话去掉。
英语的逻辑就是三个0为一个大单位,然后每个大单位里面又分为个位十位和百位,规律还是蛮好找的。
num1 = ['zero','one','two','three','four','five','six',
'seven','eight','nine','ten','eleven','twelve',
'thirteen','fourteen','fifteen','sixteen',
'seventeen','eighteen','nineteen']
num2 = ['','','twenty','thirty','forty','fifty','sixty',
'seventy','eighty','ninety']
bum3 = ['thousand','million','billion']
while 1:
try:
n = int(input())
count,wei = 1,0
str1 = ''
while n :
if count == 1 and 0 < n%100 <= 19:
str1 = num1[n%100] + ' ' + str1
n = n//100
count = count + 2
continue
english = n%10
if count == 1 and english:
str1 = num1[english] + ' ' + str1
elif count == 2 and english:
str1 = num2[english] + ' ' + str1
elif count == 3 and english:
str1 = num1[english] + ' ' +'hundred' + ' ' + 'and' + ' ' + str1
elif count == 4:
count = 2
if english:
str1 = num1[english] + ' ' + bum3[wei] + ' ' + str1
else:#如果大单位(即,前面。如10,000)是0的话
str1 = bum3[wei] + ' ' + str1
wei = wei + 1
n = n//10
continue
n = n//10
count = count + 1
if not str1:
str1 = num1[n]
elif str1.rstrip()[-3:] == 'and':
str1 = str1.rstrip()[:-3]
print(str1.rstrip())
except:
break