题解 | #人民币转换#

人民币转换

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

num2signal = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌",
              "玖", "拾", "佰", "仟", "万", "亿", "元", "角", "分", "零", "整"]

complete, coin = input().split('.')

fen = coin[1]
jiao = coin[0]
coin_s = ""
if int(jiao) > 0:
    coin_s += num2signal[int(jiao)]+"角"
if int(fen) > 0:
    coin_s += num2signal[int(fen)]+"分"


def read_4(s):
    nums = [int(i) for i in s]
    l_nums = len(nums)
    out = ""
    if l_nums == 4:
        if nums[-4]:
            out += num2signal[nums[-4]]+"仟"
        else:
            out += "零"
    if l_nums >= 3:
        if nums[-3]:
            out += num2signal[nums[-3]]+"佰"
        else:
            out += "零"
    if l_nums >= 2:
        if nums[-2] == 0 and nums[-1]:
            out = out + "零" + num2signal[nums[-1]]
        elif nums[-2] == 1 and nums[-1] == 0:
            out = out + "拾"
        elif nums[-2] == 1:
            out = out + "拾" + num2signal[nums[-1]]
        elif nums[-2] > 1:
            out = out + num2signal[nums[-2]] + "拾" + num2signal[nums[-1]]
    elif l_nums == 1:
        if nums[-1]:
            out += num2signal[nums[-1]]

    return out


L = len(complete)
complete_s = ""
if L <= 4:
    complete_s += read_4(complete)

elif L <= 8:
    complete_s += read_4(complete[:-4])+"万"
    complete_s += read_4(complete[-4:])

elif L <= 12:
    complete_s += read_4(complete[:-8])+"亿"
    complete_s += read_4(complete[-8:-4])+"万"
    complete_s += read_4(complete[-4:])

if complete_s:
    complete_s += "元"

if not coin_s:
    out = "人民币" + complete_s + "整"
else:
    out = "人民币" + complete_s+coin_s

rout = ""
for i in  range(len(out)):
    if out[i]  == "零" and out[i+1]  == "零":
        continue
    else:
        rout += out[i]



print(rout)

全部评论

相关推荐

头像
11-09 12:17
清华大学 C++
out11Man:小丑罢了,不用理会
点赞 评论 收藏
分享
听说改名字就能收到offer哈:Radis写错了兄弟
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务