题解 | #人民币转换#

人民币转换

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

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务