题解 | #人民币转换#
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
与题目【HJ42 学英语】类似,学英语是三位拆分,本题是四位拆分
def trans(n):
s=n.rjust(4,'0')
l=["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"]
res=""
for i in range(4):
if i==0 and s[i]!='0':
res+=l[int(s[i])]+"仟"
if i==1 and s[i]!='0':
res+=l[int(s[i])]+"佰"
if i==2 and s[i]!='0':
if s[i-1]=='0' and s[i-2]!='0':
if s[i]=='1':
res+="零拾"
else:
res+="零"+l[int(s[i])]+"拾"
else:
if s[i]=='1':
res+="拾"
else:
res+=l[int(s[i])]+"拾"
if i==3 and s[i]!='0':
res+=l[int(s[i])]
return res
l=["零","壹","贰","叁","肆","伍","陆","柒","捌","玖"]
z,f=input().split('.')
nums=[]
ans='人民币'
for i in range(len(z),0,-4):
a=max(0,i-4) #当起点变为复数时,取0
nums.insert(0,z[a:i])
for i in range(len(nums)):
n=nums.pop(0)
if len(nums)==2:
ans+=trans(n)+"亿"
if len(nums)==1:
ans+=trans(n)+"万"
if len(nums)==0 and z!='0':
ans+=trans(n)+"元"
if f[0]!='0':
ans+=l[int(f[0])]+"角"
if f[1]!='0':
ans+=l[int(f[1])]+"分"
if f[0]=='0' and f[1]=='0':
ans+="整"
print(ans)


