题解 | #人民币转换#
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
import sys def num_china(n): if n == 1: return "壹" if n == 2: return "贰" if n == 3: return "叁" if n == 4: return "肆" if n == 5: return "伍" if n == 6: return "陆" if n == 7: return "柒" if n == 8: return "捌" if n == 9: return "玖" if 10 <= n < 100: if n//10!=1: s = f"{num_china(n//10)}拾" else: s="拾" return s+num_china(n%10) if 100 <= n < 1000: s = f"{num_china(n//100)}佰" if 0 < n - (n // 100)*100 < 10: s += "零" return s+num_china(n%100) if 1000 <= n < 10000: s = f"{num_china(n//1000)}仟" if 0 < n - (n // 1000) *1000< 100: s += "零" return s+num_china(n%1000) if 10000 <= n < 100000000: s = f"{num_china(n//10000)}万" if 0 < n - (n // 10000)*10000 < 1000: s += "零" return s+num_china(n%10000) if 100000000 <= n: s = f"{num_china(n//100000000)}亿" if 0 < n - (n // 100000000)*100000000< 1000 or 10000<=n - (n // 100000000)*100000000<=10000000: s += "零" return s+num_china(n%100000000) for line in sys.stdin: a = line.split() n=a[0] sum="人民币" zhengshu=int(float(n)) # print(num_china(zhengshu) # print(str(num_china(zhengshu))) xiaoshu=float(n)-zhengshu xiaoshu=0 try: xiaoshu=n.split(".")[1] except: continue if zhengshu: sum+=str(num_china(zhengshu)) sum+="元" if xiaoshu: # print(xiaoshu) tmp_xiaoshu=int(xiaoshu) # print(tmp_xiaoshu) if tmp_xiaoshu//10: sum+=f"{str(num_china(tmp_xiaoshu//10))}角" if tmp_xiaoshu%10: sum+=f"{str(num_china(tmp_xiaoshu%10))}分" else: sum+="整" print(sum)