题解 | #人民币转换#
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
import sys ch_word = "零、壹、贰、叁、肆、伍、陆、柒、捌、玖".split("、") digit =['仟', '佰', '拾', ''] # 中文大写金额数字到“元”为止的,在“元”之后,应写“整字 # 阿拉伯数字中间有“0”时,中文大写要写“零”字,阿拉伯数字中间连续有几个“0”时,中文大写金额中间只写一个“零”字 # 十万以上的数字接千不用加“零”,例如,30105000.00应写作“人民币叁仟零拾万伍仟元整 def solve_thousand(string): if set(string) == "0": return "零" string = string.zfill(4) new_str = "" flag = 1 for x in range(len(string)): if string[x] == "0": if flag == 0: new_str += "零" flag ^= 1 continue else: flag = 0 if x == 2 and string[x] == '1': #10应写作“拾”,100应写作“壹佰” new_str += digit[x] continue new_str += ch_word[int(string[x])] new_str += digit[x] return new_str def solve_int_str(string: str) -> str: n = len(string) if n > 8: return ( solve_int_str(string[:-8]) + "亿" + solve_int_str(string[-8:-4]) + "万" + solve_int_str(string[-4:]) ) if n > 4: return solve_int_str(string[:-4]) + "万" + solve_int_str(string[-4:]) return solve_thousand(string) def solve_double(string): if string == "00": return "整" str_0 ='' if string[0] != "0": str_0 = ch_word[int(string[0])] + "角" str_1 = '' if string[1] != "0": str_1 = ch_word[int(string[1])] + "分" return str_0 + str_1 w = input().split(".") a_part = solve_int_str(w[0]) if a_part != '': a_part+="元" print("人民币" + a_part + solve_double(w[1]))
没啥好说的,就是递归到千以内进行解决