题解 | #人民币转换#
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
def fun(n, s=""):
if n < 20: # 由于10应写作“拾”,所以第一前1-19进行查字典处理
s += dic[n]
elif n < 100: # 大于20小于100的数
if n % 10 >= 1: # 非整十
s += fun(n // 10) + "拾" + fun(n % 10)
else: # 整十
s += fun(n // 10) + "拾"
elif n < 1000: # 大于100小于1000的数
if n % 100 >= 10: # 十位不为0
s += fun(n // 100) + "佰" + fun(n % 100)
elif n % 100 > 0: # 个位不为零
s += fun(n // 100) + "佰零" + fun(n % 100)
else: # 个位为零
s += fun(n // 100) + "佰"
elif n < 10000: # 大于1000小于10000的数
if n % 1000 >= 100: # 百位不为零
s += fun(n // 1000) + "仟" + fun(n % 1000)
elif n % 1000 > 0: # 个位不为0
s += fun(n // 1000) + "仟零" + fun(n % 1000)
else: # 个位为0
s += fun(n // 1000) + "仟"
elif n < 100000000: # 大于10000小于100000000的数
if n % 10000 >= 1000: # 千位不为0时
s += fun(n // 10000) + "万" + fun(n % 10000)
elif n % 10000 > 0: # 个位不为0
s += fun(n // 10000) + "万零" + fun(n % 10000)
else: # 个位为0
s += fun(n // 10000) + "万"
else: # 大于100000000的数
if n % 100000000 >= 10000000: # 千万位不为0
s += fun(n // 10000) + "亿" + fun(n % 100000000)
elif n % 100000000 > 0: # 个位不为0
s += fun(n // 100000000) + "亿零" + fun(n % 100000000)
else: # 个位为0
s += fun(n // 100000000) + "亿"
return s
while True:
try:
dic = {
1: "壹",
2: "贰",
3: "叁",
4: "肆",
5: "伍",
6: "陆",
7: "柒",
8: "捌",
9: "玖",
10: "拾",
11: "拾壹",
12: "拾贰",
13: "拾叁",
14: "拾肆",
15: "拾伍",
16: "拾陆",
17: "拾柒",
18: "拾捌",
19: "拾玖",
}
n, f = map(int, input().split("."))
if n > 0:
s = "人民币" + fun(n) + "元"
else:
s = "人民币"
if f == 0:
s += "整"
elif f < 10:
s += dic[f] + "分"
elif f % 10 == 0:
s += dic[f // 10] + "角"
else:
s += dic[f // 10] + "角" + dic[f % 10] + "分"
print(s)
except:
break
