dic = {2:['','','Twenty','Thirty','Forty','Fifty','Sixty','Seventy','Eighty','Ninety'],
3:['','One','Two','Three','Four','Five','Six','Seven','Eight','Nine'],
1:['Ten','Eleven','Twelve','Thirteen','Fourteen','Fifteen','Sixteen',
'Seventeen','Eighteen','Nineteen'],
0:['','Thousand','Million','Billion']}
def solve(n):
num = [int(x)for x in str(n)]
temp = [num[(i-3 if i-3>=0 else 0):i] for i in range(len(num),0,-3)]
res = ''
for i in range(len(temp)):
while len(temp[i])<3:
temp[i].insert(0,0)
trs = [dic[3][temp[i][0]]]
if temp[i][1] == 1:
trs.append(dic[1][temp[i][2]])
else:
trs.extend([dic[2][temp[i][1]],dic[3][temp[i][2]]])
if trs[0]:
if trs[1] or trs[2]:
trs.insert(1,'And')
trs.insert(1,'Hundred')
elif (not i) and (trs[1] or trs[2]):
trs.insert(1,'And')
while '' in trs:
trs.remove('')
if trs:
if i:trs.append(dic[0][i])
if res:
res = ' '.join(trs)+' '+res
else:
res = ' '.join(trs)
return res if res else 'Zero'
if __name__ == '__main__':
print('示例1 输出:',repr(solve(123)))
print('示例2 输出:',repr(solve(12345)))
print('示例3 输出:',repr(solve(1234567)))
print('示例4 输出:',repr(solve(1234567891)))
print('之前多空格的 输出:',repr(solve(1000010)))