题解 | #人民币转换#
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
ls = "零壹贰叁肆伍陆柒捌玖" x = "元拾佰仟万亿角分整" # 万元以内 def read_thousand(num:int)-> str: one = "" ten = "" hundred = "" thounsand = "" res_ls = [thounsand,hundred,ten,one] for i in range(1,5): k = int(num%10) num = num//10 if k : res_ls[-i] = ls[k] + x[i-1] #当中间存在0的时候,添加一个零 temp = 0 c = 0 for i in range(len(res_ls)): if temp != len(res_ls[i]): c += 1 temp = len(res_ls[i]) if c == 2: res_ls[i] = ls[0] c += 1 if res_ls[-1] != "" or res_ls[-1] == ls[0]: return "".join(res_ls)[:-1] else: return "".join(res_ls) #去掉0或者去掉元 string = "" def app(num:int) ->str: string = "" length = len(str(num)) if length <= 4: string = read_thousand(num) elif length <= 8: re = int(num % 10e3) dt = int(num // 10e3) string = app(dt) + x[4] + app(re) else: re = int(num % 10e7) dt = int(num // 10e7) string = app(dt) + x[5] + app(re) return string num = float(input()) res = "人民币" #小数部分 laststr = "" if int(round(num * 100) % 100) == 0: laststr = x[-1] else: a = int(round(num*100) % 100) #a // 10 为多少角 if a//10 != 0: laststr += ls[a//10] + x[6] #a % 10 为多少分 if a % 10 != 0: laststr += ls[a%10] + x[7] #整数部分 bignum = int(num * 100 //100) bignumstr = app(bignum).replace("壹拾","拾") if int(num) == 0 and int(num * 100 % 100) == 0: print("人民币零元整") elif len(bignumstr) == 0: finalstr =res + laststr print(finalstr) else: finalstr =res + bignumstr +"元" + laststr print(finalstr)