ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

牛客华为机试HJ95

2022-04-22 07:31:05  阅读:170  来源: 互联网

标签:10 do nums res 牛客 append 机试 HJ95 dot


原题传送门

1. 问题描述

2. Solution

import sys

if sys.platform != "linux":
    sys.stdin = open("input/HJ95.txt")

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

"""
拾、佰、仟、万、亿、元、角、分

3 2315 1121 .15
人民币
叁 亿
贰仟叁佰壹拾伍 万
壹仟壹佰贰拾壹 元
壹角 伍分



1 0012 .02
叁万
零
拾贰

15 1121 .15
人民币
拾伍万
壹仟壹佰贰拾壹元
壹角伍分

人民币叁仟零拾万伍仟零元整	
人民币叁仟零拾万伍仟元整

"""


def do_part(n, res: list):
    a = n % 10
    b = (n // 10) % 10
    c = (n // 100) % 10
    d = n // 1000
    if d != 0:
        res.append(nums[d])
        res.append('仟')
    elif res[-1] != "人民币":
        res.append('零')

    if c != 0:
        res.append(nums[c])
        res.append('佰')
    elif d != 0 and c == 0 and (b != 0 or a != 0):
        res.append('零')

    if b != 0:
        if b != 1 and res[-1] != "人民币":
            res.append(nums[b])
        res.append('拾')
    elif d != 0 and c != 0 and b == 0:
        res.append('零')

    if a != 0:
        res.append(nums[a])


def do_after_dot(n, res: list):
    a = n % 10
    b = n // 10

    if b != 0:
        res.append(nums[b])
        res.append('角')
    if a != 0:
        res.append(nums[a])
        res.append('分')


def solve(s):
    before_dot, after_dot = list(map(int, s.split(".")))

    res = ['人民币']
    a = before_dot % 10000  # 元部分
    b = (before_dot // 10000) % 10000  # 万部分
    c = before_dot // 100000000  # 亿部分

    if c > 0:
        do_part(c, res)
        res.append('亿')
    if b > 0:
        do_part(b, res)
        res.append('万')
    if a > 0:
        do_part(a, res)
        res.append('元')
    if after_dot == 0:
        res.append('整')
    else:
        do_after_dot(after_dot, res)
    print("".join(res))


for line in sys.stdin:
    solve(line.strip())

标签:10,do,nums,res,牛客,append,机试,HJ95,dot
来源: https://www.cnblogs.com/junstat/p/16177319.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有