HJ42 学英语 递归解法

 1 #1271919
 2 d={1:'one',2:'two',3:'three',4:'four',5:'five',
 3    6:'six',7:'seven',8:'eight',9:'nine',10:'ten',
 4    11:'eleven',12:'twelve',13:'thirteen',14:'fourteen',15:'fifteen',
 5    16:'sixteen',17:'seventeen',18:'eighteen',19:'nineteen',20:'twenty',
 6    30:'thirty',40:'forty',50:'fifty',60:'sixty',70:'seventy',
 7    80:'eighty',90:'ninety',100:'hundred',
 8    1000:'thousand',1002:'billion',1001:'million','a':'and'}
 9 def three(t,tag=1000,l=[],l1=[],flag=1):
10     t1=t[-3:][::-1]
11     if len(t)>=3:
12         if int(t1[2])==0:
13             pass
14         else:
15             l.append(d[int(t1[2])])
16             l.append(d[100])
17             if int(t1[1])==0 and int(t1[0])==0:
18                 pass
19             else:
20                 l.append(d['a'])
21     if len(t)>=2:
22         if int(t1[1])==0:
23             pass
24         elif int(t1[1])!=0 and int(t1[0])==0:
25             l.append(d[int(t1[1])*10])
26             flag=0
27         elif int(t1[1])==1:
28             l.append(d[int(t1[1])*10+int(t1[0])])
29             flag=0
30         else:
31             l.append(d[int(t1[1])*10])
32     if flag==1:
33         if int(t1[0])==0:
34             pass
35         else:
36             l.append(d[int(t1[0])])
37     l1=l.copy()+l1
38     #print("l1",l1)
39     if t[:-3]=='':
40         return l1
41     while t[:-3]!='':
42         l1.insert(0,d[tag])
43         tag+=1
44         t=t[:-3]
45         #print("tag",tag)
46         return three(t,l=[],tag=tag,l1=l1)
47 
48 n=input()
49 l1=three(n)
50 print(" ".join(i for i in l1))

 

posted @ 2023-04-26 17:57  Aneverforget  阅读(22)  评论(0编辑  收藏  举报