题解 | #学英语#
学英语
https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
考虑到三位一循环的结构,采取递归方法,希望用‘ ’.join()来输出结果,但是实际上输出时会出现格式错
遂借助split替换重复空格
num1 = ['','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'] def word(n): if n<20: return [num1[n]] if n<100: return [num2[n//10]] + [num1[n%10]] if n<1000: if n%100==0: return [num1[n//100]] + ['hundred'] else: return [num1[n//100]] + ['hundred and'] + word(n%100) for p,w in enumerate(['thousand','million','billion']): if n<1000**(p+2): return word(n//1000**(p+1)) + [w] + word(n%1000**(p+1)) while True: try: num = int(input()) if num == 0 : print('zero') else: print(' '.join((' '.join(word(num))).split())) except: break