理解型+数位判断 | HJ95 人民币转换
reflect = { '0':'零', '1':'壹', '2':'贰', '3':'叁', '4':'肆', '5':'伍', '6':'陆', '7':'柒', '8':'捌', '9':'玖', '10':'拾', '100':'佰', '1000':'仟', '10000':'万', '100000000':'亿', } def func1(money): if money == '0': return '' i = 1 m = money[-4:] ret = [] while m: wei = int('1'+(len(m)-1)*'0') # 除数位跟着四位数组实际长度走 res = '' while m: # if int(m) >= wei: if int(m)//wei != 0: n = reflect[str(int(m)//wei)] if wei==10 and str(int(m))[0] == '1': res += '{}'.format(reflect[str(wei)]) elif wei != 1: w = reflect[str(wei)] if wei != 1 else '' res += '{}{}'.format(n, w) else: res += '{}'.format(n) else: res += '{}'.format(reflect['0']) m = m[1:] wei //= 10 # 去掉尾部连续0 if res[-3:]=='零零零': res = res[:-3] elif res[-2:]=='零零': res = res[:-2] elif res[-1]=='零': res = res[:-1] # res = res.replace('零', '') if i > 1: # 每四位数作为一组,当为"万"组时结尾加"万"单位 danwei = reflect['1'+4*(i-1)*'0'] res += danwei ret.append(res) i += 1 m = money[i*(-4):(i-1)*(-4)] # 推进到下一个四位数组 # m = '{:0>4}'.format() return ''.join(ret[::-1]) + '元' # 倒序拼接整数部分 def func2(m): if m != 0: j, f = m//10, m%10 if f != 0 and j != 0: return '{}角{}分'.format(reflect[str(j)], reflect[str(f)]) elif f == 0: return '{}角'.format(reflect[str(j)]) else: return '{}分'.format(reflect[str(f)]) else: return '整' while True: try: m1, m2 = input().split('.') print('人民币'+func1(m1)+func2(int(m2))) except: break
用时:1.5h
华为笔试刷题 文章被收录于专栏
高质量题: 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