题解 | #学英语#
学英语
https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
import sys for line in sys.stdin: n = line.strip('\n') # print(n) num = [i for i in range(1,20)] + [i for i in range(20, 100, 10)] words = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten',\ 'eleven', 'twelve', 'thirtee', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen',\ 'twenty','thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety' ] dic = {i:j for i, j in zip(num, words)} # print(dic) lst = [] n1 = n[:] while len(n1) > 0: lst.append(int(n1)%1000) n1 = n1[:-3] # print(lst) # hundred, thousand, million, billion, and, zero def translate(x): res = '' if x < 100: if 0 < x <= 20 or x % 10 == 0: res += dic[x] else: res += dic[x//10*10]+ ' ' + dic[x%10] elif 100 <= x < 1000: if x % 100 == 0: res += dic[x//100] + ' hundred' else: res += dic[x//100] + ' hundred and ' + translate(x%100) return res tail = {0:'', 1:' thousand ', 2:' million ', 3:' billion '} def integrate(lst): result = [] res = '' for i in range(len(lst)): x = lst[i] if i in (0, 1, 2): result.append(translate(x) + tail[i]) else: result.append(translate(x) + tail[i] + 'and ') for i in result[::-1]: res += i return res try: res = integrate(lst) print(res) except Exception as error: print(error)