题解 | #学英语#
学英语
https://www.nowcoder.com/practice/1364723563ab43c99f3d38b5abef83bc
对输入字符型数字,从右往左提取每三位进行处理,以字符串长度整除3的值作为循环次数判断
d = { '1': 'one','2': 'two','3':'three', '4':'four', '5':'five', '6':'six', '7':'seven', '8':'eight', '9':'nine', '10':'ten', '11':'eleven', '12':'twelve', '13':'thirteen', '14':'fourteen', '15':'fifteen', '16':'sixteen', '17':'seventeen', '18':'eighteen', '19':'nineteen', '20':'twenty', '30':'thirty', '40':'forty', '50':'fifty', '60':'sixty', '70':'seventy', '80':'eighty', '90':'ninety', '100': 'hundred', '1000': 'thousand', '1000000': 'million', '1000000000': 'billion', } while True: try: num = input() res = [] i = 0 while i <= len(num)//3: # 字符串数字,从右到左每三位进行处理 # 获取分段三位(注意num[-3:0]无效) seg = num[-3:] if i==0 else num[-3*(i+1):-3*i] if seg == '': # 分段为空推出循环 break if i >= 1: # 分段从i=1开始处理thousand、million等 seg_digit = '1' + '000'*i res.append(d[seg_digit]) # 分段三位不满三位向左填充0 seg = '{:0>3}'.format(seg) ge, shi, bai = seg[2], seg[1], seg[0] # 个位、十位、百位 if ge != '0' and shi != '1': # 个位不为0且十位不为1,nineteen等特殊处理 res.append(d[ge]) if shi != '0': if shi == '1': # 十位为1,用seg[1:]十位+个位获取英文 res.append(d[seg[1:]]) else: res.append(d[f'{shi}0']) if bai != '0': if shi!='0' or ge!='0': # 百位后无十位或个位则不需要添加and res.append('and') res.append(f'{d[bai]} hundred') i += 1 print(' '.join(res[::-1])) except: break
华为笔试刷题 文章被收录于专栏
高质量题: 1~40:HJ16,HJ22,HJ24,HJ26,HJ27,HJ28,HJ35,HJ37,HJ39; 40~80:HJ41,HJ42,HJ43,HJ44,HJ48,HJ50,HJ52,HJ53,HJ57,HJ61,HJ63,HJ64,HJ70,HJ71,HJ74,HJ77; 80~108:HJ82,HJ85,HJ88,HJ89,HJ93,HJ95,HJ98,HJ103,HJ107