题解 | #人民币转换#
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
dic = { 1: "壹", 2: "贰", 3: "叁", 4: "肆", 5: "伍", 6: "陆", 7: "柒", 8: "捌", 9: "玖", 0: "零", } def foursite(s0000): res = "" if s0000[0] != 0 and s0000[1] == s0000[2] == s0000[3] == 0: res = dic[s0000[0]] + "仟" elif s0000[0] != 0 and s0000[1] != 0 and s0000[2] == s0000[3] == 0: res = dic[s0000[0]] + "仟" + dic[s0000[1]] + "佰" elif s0000[0] != 0 and s0000[1] != 0 and s0000[2] != 0 and s0000[3] == 0: res = dic[s0000[0]] + "仟" + dic[s0000[1]] + "佰" + dic[s0000[2]] + "拾" elif s0000[0] != 0 and s0000[1] != 0 and s0000[2] != 0 and s0000[3] != 0: res = ( dic[s0000[0]] + "仟" + dic[s0000[1]] + "佰" + dic[s0000[2]] + "拾" + dic[s0000[3]] ) return res def threesite(s000): res = "" if s000[1] == s000[2] == 0: res += dic[s000[0]] + "佰" elif s000[1] != 0 and s000[2] == 0: res += dic[s000[0]] + "佰" + dic[s000[1]] + "拾" elif s1[1] != 0 and s1[2] != 0: res += dic[s000[0]] + "佰" + dic[s000[1]] + "拾" + dic[s000[2]] return res def twosite(s00): res = "" if s00[0] != 1: if s00[1] == 0: res += dic[s00[0]] + "拾" elif s00[1] != 0: res += dic[s00[0]] + "拾" + dic[s00[1]] elif s00[0] == 1: res += '拾' + dic[s00[1]] return res def xiaoshu(s2): res = "" if s2[0] == s2[1] == 0: res += "整" elif s2[0] != 0 and s2[1] == 0: res += dic[s2[0]] + "角" elif s2[0] != 0 and s2[1] != 0: res += dic[s2[0]] + "角" + dic[s2[1]] + "分" elif s2[0] == 0 and s2[1] != 0: res += dic[s2[1]] + "分" return res while 1: try: money = input() s1 = list(map(int, list(str(money.split(".")[0])))) s2 = list(map(int, list(str(money.split(".")[1])))) # print(s1,s2) ress = "人民币" if len(s1) == 1 and s1[0] != 0: ress += dic[s1[0]] + "元" + xiaoshu(s2) elif len(s1) == 1 and s1[0] == 0: ress += xiaoshu(s2) elif len(s1) == 2: ress += twosite(s1) + "元" + xiaoshu(s2) elif len(s1) == 3: ress += threesite(s1) + "元" + xiaoshu(s2) elif len(s1) == 4: ress += foursite(s1) + "元" + xiaoshu(s2) elif len(s1) == 5: if s1[1] != 0: ress += dic[s1[0]] + "万" + foursite(s1[-4::]) + xiaoshu(s2) elif s1[1] == 0 and s1[2] != 0: ress += dic[s1[0]] + "万" + "零" + threesite(s1[-3::]) + xiaoshu(s2) elif s1[1] == s1[2] == 0 and s1[3] != 0: ress += dic[s1[0]] + "万" + "零" + twosite(s1[-3::]) + xiaoshu(s2) elif s1[1] == s1[2] == s1[3] == 0 and s1[-1] != 0: ress += dic[s1[0]] + "万" + "零" + dic[s1[-1]] + "元" + xiaoshu(s2) elif s1[1] == s1[2] == s1[3] == s1[-1] == 0: ress += dic[s1[0]] + "万" + "元" + xiaoshu(s2) elif len(s1) == 6: if s1[2] != 0: ress += twosite(s1[:2:]) + "万" + foursite(s1[-4::]) + "元" + xiaoshu(s2) elif s1[2] == 0 and s1[3] != 0: ress += ( twosite(s1[:2:]) + "万" + "零" + threesite(s1[-3::]) + "元" + xiaoshu(s2) ) elif s1[2] == s1[3] == 0 and s1[4] != 0: ress += ( twosite(s1[:2:]) + "万" + "零" + twosite(s1[-3::]) + "元" + xiaoshu(s2) ) elif s1[2] == s1[3] == s1[4] == 0 and s1[5] != 0: ress += twosite(s1[:2:]) + "万" + "零" + dic[s1[-1]] + "元" + xiaoshu(s2) elif s1[2] == s1[3] == s1[4] == s1[5] == 0: ress += twosite(s1[:2:]) + "万" + "元" + xiaoshu(s2) elif len(s1) == 7: if s1[3] != 0: ress += ( threesite(s1[:3:]) + "万" + foursite(s1[-4::]) + "元" + xiaoshu(s2) ) elif s1[3] == 0 and s1[4] != 0: ress += ( threesite(s1[:3:]) + "万" + "零" + threesite(s1[-3::]) + "元" + xiaoshu(s2) ) elif s1[3] == s1[4] == 0 and s1[5] != 0: ress += ( threesite(s1[:3:]) + "万" + "零" + twosite(s1[-3::]) + "元" + xiaoshu(s2) ) elif s1[3] == s1[4] == s1[5] == 0 and s1[6] != 0: ress += threesite(s1[:3:]) + "万" + "零" + dic[s1[-1]] + "元" + xiaoshu(s2) elif s1[3] == s1[4] == s1[5] == s1[6] == 0: ress += threesite(s1[:3:]) + "万" + "元" + xiaoshu(s2) elif len(s1) == 8: if s1[4] != 0: ress += ( foursite(s1[:4:]) + "万" + foursite(s1[-4::]) + "元" + xiaoshu(s2) ) elif s1[4] == 0 and s1[5] != 0: ress += ( foursite(s1[:4:]) + "万" + "零" + threesite(s1[-3::]) + "元" + xiaoshu(s2) ) elif s1[4] == s1[5] == 0 and s1[6] != 0: ress += ( foursite(s1[:4:]) + "万" + "零" + twosite(s1[-3::]) + "元" + xiaoshu(s2) ) elif s1[4] == s1[5] == s1[6] == 0 and s1[7] != 0: ress += foursite(s1[:4:]) + "万" + "零" + dic[s1[-1]] + "元" + xiaoshu(s2) elif s1[4] == s1[5] == s1[6] == s1[7] == 0: ress += foursite(s1[:4:]) + "万" + "元" + xiaoshu(s2) print(ress) except: break