题解 | #人民币转换#

人民币转换

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)

全部评论

相关推荐

11-15 17:19
湖南大学 Java
成果成果成果果:这是哪个公司的hr,这么离谱吗,我没见过用性别卡技术岗的,身边女性同学拿大厂offer的比比皆是
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
11-27 10:52
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务