题解 | #人民币转换#

人民币转换

http://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b

以万、亿为单位分隔

lst = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖']


def ch(s):
    res = ''
    if len(s) == 4:
        if s[0] != '0':
            res += lst[int(s[0])] + '仟'
        else:
            if s[1] != '0':
                res += lst[int(s[0])]
        if s[1] != '0':
            res += lst[int(s[1])] + '佰'
        else:
            if s[2] != '0':
                res += lst[int(s[1])]
        if s[2] != '0':
            if s[2] != '1':
                res += lst[int(s[2])] + '拾'
            else:
                res += '拾'
        else:
            if s[3] != '0':
                res += lst[int(s[2])]
        if s[3] != '0':
            res += lst[int(s[3])]
    elif len(s) == 3:
        if s[0] != '0':
            res += lst[int(s[0])] + '佰'
        else:
            res += lst[int(s[0])]
        if s[1] != '0':
            if s[1] != '1':
                res += lst[int(s[1])] + '拾'
            else:
                res += '拾'
        else:
            res += lst[int(s[1])]
        if s[2] != '0':
            res += lst[int(s[2])]
    elif len(s) == 2:
        if s[0] != '0':
            if s[0] != '1':
                res += lst[int(s[0])] + '拾'
            else:
                res += '拾'
        else:
            res += lst[int(s[0])]
        if s[1] != '0':
            res += lst[int(s[1])]
    elif len(s) == 1:
        if s[0] != '0':
            res += lst[int(s[0])]
    return res


while True:
    try:
        lst1, lst2 = input().split('.')
        res1, res2, res3 = '人民币', '', ''
        if lst2 == '00':
            res3 = '元整'
        else:
            if lst1 != '0':
                res3 = '元'
            if lst2[0] != '0':
                res3 += lst[int(lst2[0])] + '角'
            if lst2[1] != '0':
                res3 += lst[int(lst2[1])] + '分'
        if len(lst1) > 8:
            res1 += ch(lst1[:-8]) + '亿'
            res1 += ch(lst1[-8:-4]) + '万'
            res2 += ch(lst1[-4:])
        elif len(lst1) > 4:
            res1 += ch(lst1[:-4]) + '万'
            res2 += ch(lst1[-4:])
        else:
            res2 += ch(lst1)
        print(res1 + res2 + res3)
    except:
        break

全部评论

相关推荐

06-27 18:53
门头沟学院 Java
这样才知道自己不适合搞代码,考公去咯
只爱喝白开水:我也发现不适合搞代码,打算转非技术方向了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
感觉他们一点都不了解现在这个社会就业有多难,已经在牛客刷到好多篇 延毕的帖子了,延毕就会导致已经找好的工作就没了,还得重新再找,学校和老师们是怎么想的呢????看到学生丢失工作会开心吗 就业数据都在造假,真实的就业困难不去解决 一个个真是好样的
从明天开始狠狠卷JV...:学生看到的是导师不放实习导致offer黄了。 导师看到的是招进来的学生吃自己补助和自己的招生名额,却没给自己升迁带来任何帮助,还要跑路。 根本利益的不一致,最主要留校的导师大概率是职场上招聘失败的,被迫留校的,什么牛鬼蛇神都会有
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务