题解 | #学英语#
学英语
https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
import sys
#三位一组划分,比如1,652,510会变成列表[1,652,510]
n = list(input())[::-1]
n_new = []
num = 0
for i in n:
if num%3 == 0 and num!=0:
n_new.append('.')
n_new.append(i)
num += 1
n_new =''.join(n_new[::-1]).split('.')
list_1 = [0,'one','two','three','four',
'five','six','seven','eight','nine',
'ten','eleven','twelve','thirteen',
'fourteen','fifteen','sixteen','seventeen',
'eighteen','nineteen']
list_2 = [0,0,'twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']
#1000以内
def l_1000(nums,connect=''):
#如果是1,088这种就去掉88前面的'and'拼接
if nums < 100:
connect = ''
result=''
if 0< nums < 20:
result = connect+list_1[int(nums)]
elif 20 <= nums < 100:
if nums % 10 ==0:
result += connect+list_2[nums//10]
else:
result += connect+list_2[nums//10] + ' ' + list_1[nums%10]
elif 100 <= nums <1000:
if nums % 100 == 0:
result += list_1[nums//100] + ' hundred'
else:
result += list_1[nums//100] + ' hundred and ' + l_1000(nums%100)
return result
#1000,000
def l_10w(nums):
return l_1000(nums) + ' thousand '
#1,000,000
def l_100w(nums):
return l_1000(nums) + ' million '
if len(n_new) == 1:
print(l_1000(int(n_new[0])))
if len(n_new) == 2:
print(l_10w(int(n_new[0]))+l_1000(int(n_new[1]),connect='and '))
if len(n_new) == 3:
print(l_100w(int(n_new[0]))+l_10w(int(n_new[1]))+l_1000(int(n_new[2]),connect='and '))
查看16道真题和解析
