题解 | #学英语#
学英语
https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
# 笨比做法
def numToEng(n):
lst1 = ['', 'one ', 'two ', 'three ', 'four ', 'five ', 'six ', 'seven ', 'eight ', 'nine ']
lst2 = ['', 'ten', 'twenty ', 'thirty ', 'forty ', 'fifty ', 'sixty ', 'seventy ', 'eighty ', 'ninety ']
lst3 = ['', 'eleven', 'twelve', 'thirteen', 'forteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen']
result = ''
l = len(n)
if l == 3:
if n[1] == '0' and n[2] == '0':
result += lst1[int(n[0])] + 'hundred'
else:
if n[1] == '1' and n[2] == '0':
result += lst1[int(n[0])] + 'hundred and ten'
elif n[1] == '1' and n[2] != '0':
result += lst1[int(n[0])] + 'hundred and ' + lst3[int(n[2])]
else:
result = result + lst1[int(n[0])] + 'hundred and ' + lst2[int(n[1])] + lst1[int(n[2])]
elif l == 2:
if n[0] == '1' and n[1] == '0':
result += 'ten'
elif n[0] == '1' and n[1] != '0':
result += lst3[int(n[2])]
else:
result += lst2[int(n[0])] + lst1[int(n[1])]
elif l == 1:
result += lst1[int(n)]
return result
s = format(int(input()), ',').split(',')
engNum = ''
n = []
for i in s:
n.append(i.lstrip('0'))
l = len(n)
if l == 4:
engNum += numToEng(n[0]) + 'billion ' + numToEng(n[1]) + 'million ' + numToEng(n[2]) + 'thousand ' + numToEng(
n[3])
elif l == 3:
engNum += numToEng(n[0]) + 'million ' + numToEng(n[1]) + 'thousand ' + numToEng(n[2])
elif l == 2:
engNum += numToEng(n[0]) + 'thousand ' + numToEng(n[1])
elif l == 1:
engNum += numToEng(n[0])
print(engNum)
查看7道真题和解析
