|
发表于 2021-6-25 02:13:18
|
显示全部楼层
本帖最后由 阿奇_o 于 2021-6-25 14:11 编辑
题目里 “一个13位内的整数”, 我的认为是: 最大12位,即 9999 9999 9999 最大。
好,看代码:
- # 思路:分别对 亿以上,万以上,万以下的,做类似的处理。小心注意处理中间各种零的情况
- # 代码实现:
- s = input()
- assert int(s) > 0
- # 分别取出 亿以上,万以上,万以下的
- sy, sw, sq = '', '', ''
- if len(s) > 8: # 一亿以上的(9到12位位数)
- sy = s[:len(s)-8]
- sw = s[len(s)-8:len(s)-4]
- sq = s[-4:]
- elif len(s) <= 8 and len(s) > 4: # 小于一亿,但大于等于一万的 (五位到八位数)
- sw, sq = s[:-4], s[-4:]
- else:
- sq = s # 小于一万的
- # 或者 简单的
- s = '0'*(12-len(s)) + s
- sy, sw, sq = s[:4], s[4:8], s[8:]
- # print(sy, sw, sq)
- # 处理仟佰拾,以及中间有零的情况
- n, zh = '0123456789', "零壹贰叁肆伍陆柒捌玖"
- def qbs(sx=''):
- if sx == '' or sx == '0000': # 处理像1234-0000-1234里的0000 (没“万”)
- return ''
- elif sx[0] == '0' or sx[:2] == '00' or sx[:3] == '000': # 处理像 0111, 0011, 0001
- return ("零"+str(int(sx))).translate(''.maketrans(n, zh))
- else:
- x = "仟佰拾" if len(sx) == 4 else "佰拾" if len(sx) == 3 else "拾" if len(sx) == 2 else ""
- t = ''.join([n + z for n, z in zip(sx[:-1], x)]) + sx[-1]
- t = t.replace("0佰0拾0", "").replace("0拾0", "").replace("0", "") # 处理像 1000, 1100, 1110
- return t.translate(''.maketrans(n, zh))
- print(
- qbs(sx=sy), "亿" if qbs(sy) != '' else "",
- qbs(sw), "万" if qbs(sw) != '' else "",
- qbs(sq), "元整" if qbs(sq) != '' else '',
- sep='')
复制代码 |
|