题解 | #人民币转换#

人民币转换

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

import sys


def process_under_hundred(val_int: int) -> str:
    result = ""
    if val_int >= 10:
        num_ten = val_int // 10
        if num_ten == 1:
            result += "拾"
        else:
            result += num2ch[num_ten] + "拾"
        if val_int % 10 != 0:
            result += num2ch[val_int % 10]
    elif val_int > 0:
        result += num2ch[val_int % 10]
    # print("under hundred")
    return result


def process_under_thousand(val_int) -> str:
    result = ""
    if val_int >= 100:
        num_hundred = val_int // 100
        result += num2ch[num_hundred] + "佰"
        if val_int % 100 >= 10:
            result += process_under_hundred(val_int % 100)
        else:
            result += "零" + process_under_hundred(val_int % 100)
    else:
        result += process_under_hundred(val_int)
    return result


def process_under_wan(val_int: int) -> str:
    result = ""
    if val_int >= 1000:
        num_thousand = val_int // 1000
        result += num2ch[num_thousand] + "仟"
        if val_int % 1000 >= 100:
            result += process_under_thousand(val_int % 1000)
        else:
            result += "零" + process_under_thousand(val_int % 1000)
    else:
        result += process_under_thousand(val_int)
    return result


def process_exceed_wan(val_int: int) -> str:
    result = ""
    if val_int >= 10000:
        num_wan = val_int // 10000
        result = process_under_wan(num_wan) + "万"
        if val_int % 10000 < 1000:
            result += "零" + process_under_thousand(val_int % 10000)
        else:
            result += process_under_wan(val_int % 10000)
    else:
        result += process_under_wan(val_int)
    return result


# 要处理变量val_int
# 假如数字小于10
# 应当先处理//亿的部分,然后处理%亿,然后处理%万,千,佰,十


def process_exceed_yi(val_int: int) -> str:
    left_yi = val_int % 100000000
    result = ""
    if left_yi >= 100000000:
        result = process_exceed_yi(left_yi)
    else:
        result = process_exceed_wan(left_yi)
    if val_int >= 100000000:
        num_yi = val_int // 100000000
        if num_yi >= 100000000:
            result = process_exceed_yi(num_yi) + "亿" + result
        else:
            result = process_exceed_wan(num_yi) + "亿" + result
    return result


num2ch = "零壹贰叁肆伍陆柒捌玖"

bignum2ch = "拾佰仟万亿"

value_str = input()
val_float = float(value_str)
val_int = int(val_float)
str_after_dot = value_str.split(".")[1]

result_prefix = "人民币"
result_after_dot = ""
result_before_dot = ""
if str_after_dot != "00":
    if str_after_dot[0] != "0":
        result_after_dot += num2ch[int(str_after_dot[0])] + "角"
    if str_after_dot[1] != "0":
        result_after_dot += num2ch[int(str_after_dot[1])] + "分"
else:
    result_after_dot = "整"

if val_int == 0:
    result_int = result_prefix
else:
    result_int = result_prefix + process_exceed_yi(val_int) + "元"
print(result_int + result_after_dot)

全部评论

相关推荐

点赞 评论 收藏
分享
03-01 21:45
中北大学 Python
孤蓝长空:请你说一下为什么你用websocket而不是http,请你说一下什么是rpc,为什么用rpc,你的rpc的传输协议是JSON,xml还是什么 请你描述一下你的鉴权流程(完整的) 我问的是第二个项目,随便问的哈哈哈
开工第一帖
点赞 评论 收藏
分享
03-14 16:04
已编辑
安徽农业大学 算法工程师
痴心的她allin秋...:啥笔试都挂怎么办,某9本考研下岸,练也没时间了,对算法也不感兴趣,大部分大厂笔试只能A0-1个😄
米哈游笔试
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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