题解 | #人民币转换#

人民币转换

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

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

while True:
    try:
        s = input()
        before, after = s.split('.')
        head, body, tail = '人民币', '', ''
        if after == '00':
            tail = '元整'
        else:
            if before == '0':
                tail = ''
            else:
                tail = '元'
            if after[0] != '0':
                tail += ch[int(after[0])] + '角'
            if after[1] != '0':
                tail += ch[int(after[1])] + '分'
        lst, i = [], len(before)
        while i - 4 >= 0:
            lst.insert(0, before[i-4:i])
            i -= 4
        if i > 0:
            lst.insert(0, before[:i])
        
        for i, num in enumerate(lst): # 如:['15', '1121']
            flag = True
            for j, c in enumerate(num):
                if c == '0':
                    if num == '0000':
                        if lst[-1] != '0000' and flag:
                            body += '零'
                            flag = False
                            continue
                    else:
                        if j != len(num)-1 and num[j+1] != '0' and flag:
                            body += '零'
                            flag = False
                            continue
                else:
                    if len(num[j:]) == 4:
                        body += ch[int(c)] + '仟'
                    elif len(num[j:]) == 3:
                        body += ch[int(c)] + '佰'
                    elif len(num[j:]) == 2:
                        if c != '1':
                            body += ch[int(c)] + '拾'
                        else:
                            body += '拾'
                    else:
                        body += ch[int(c)]
            if len(lst[i:]) == 3:
                body += '亿'
            elif len(lst[i:]) == 2 and num != '0000':
                body += '万'
        print(head + body + tail)

    except:
        break
全部评论
小声哔哔,在c!=0时应该重置flag为TRUE,不然10101这种情况会少零
点赞 回复 分享
发布于 2022-04-26 11:11
flag是用来表示什么的
点赞 回复 分享
发布于 2022-04-15 23:26
if j != len(num)-1 and num[j+1] != '0' and flag: ——————————这句会不会有问题啊,j本来可取值范围是 0,1,2,3.假设 num 是个 4位数 ‘1005’,j=1,则j != len(num)-1,但是 num[j+1] = '0',可是这里还是需要加‘零’?如果按照题目里面的,这里就会少加了一个 ‘零’…… 仅供参考,哪位朋友可以一起讨论下……
点赞 回复 分享
发布于 2022-03-20 21:38

相关推荐

叶扰云倾:进度更新,现在阿里云面完3面了,感觉3面答得还行,基本都答上了,自己熟悉的地方也说的比较细致,但感觉面试官有点心不在焉不知道是不是不想要我了,求阿里收留,我直接秒到岗当阿里孝子,学校那边的房子都退租了,下学期都不回学校,全职猛猛实习半年。这种条件还不诱人吗难道 然后现在约到了字节的一面和淘天的复活赛,外加猿辅导。华为笔试完没动静。 美团那边之前投了个base广州的,把我流程卡麻了,应该是不怎么招人,我直接简历挂了,现在进了一个正常的后端流程,还在筛选,不知道还有没有hc。
点赞 评论 收藏
分享
自学java狠狠赚一...:骗你点star的,港卵公司,记得把star收回去
点赞 评论 收藏
分享
评论
7
2
分享

创作者周榜

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