题解 | #学英语#

学英语

https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc

# 输入的long型整数,不用,分开
while 1:
    try:
        n_long=input()
        yu=len(n_long)%3
        n=len(n_long)//3
        #print(list(range(0,3*2,3)))
        if yu!=0:
            n_long1=[n_long[:yu]]
        else:
            n_long1=[]
        for i in range(0,3*n,3):
            a=n_long[yu:][i]+n_long[yu:][i+1:i+3]
            #print(a)
            n_long1.append(a)
        #print(n_long1)

        map1=['','one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen']
        map2=['','','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']

        trans=[]
        for i in n_long1:
            n=i.zfill(3)
            tran=[]

            if n[0]!='0':
                tran.extend([map1[int(n[0])],'hundred'])
                if int(n[1:])==0:
                    tran = tran
                elif 0<int(n[1:])<20:
                    tran.extend(['and',map1[int(n[1:])]])
                elif int(n[1:])>=20:
                    tran.extend(['and',map2[int(n[1])]])
                    if n[2]!='0':
                        tran.append(map1[int(n[2])])
            else:
                tran = tran
                if int(n[1:])==0:
                    tran = tran
                elif 0<int(n[1:])<20:
                    tran.append(map1[int(n[1:])])
                elif int(n[1:])>=20:
                    tran.append(map2[int(n[1])])
                    if n[2]!='0':
                        tran.append(map1[int(n[2])])
            
            #print(tran)

            trans.append(' '.join(tran))
        #print(trans)

        English=''
        if len(trans)==4:
            English = trans[0]+' '+'billion'+' '+trans[1]+' '+'million'+' '+trans[2]+' '+'thousand'+' '+trans[3]
        elif len(trans)==3:
            English = trans[0]+' '+'million'+' '+trans[1]+' '+'thousand'+' '+trans[2]
        elif len(trans)==2:
            English = trans[0]+' '+'thousand'+' '+trans[1]
        elif len(trans)==1:
            English = trans[0]
        print(English)
             
    except:
        break

'''
s='1234567'
print(s[::-3][::-1])
yu=len(s)%3
n=len(s)//3
#print(list(range(0,3*2,3)))
a_l=[s[:yu]]
for i in range(0,3*n,3):
    a=s[yu:][i]+s[yu:][i+1:i+3]
    print(a)
    a_l.append(a)
print(a_l)'''

全部评论

相关推荐

点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务