题解 | #人民币转换#
人民币转换
https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
import re while 1: try: s=input().split('.') n_int=s[0][::-1] #为操作简单,可以先将整数部分倒序 n_deci=s[1] #print(n_int,n_deci) r1=['零','壹','贰','叁','肆','伍','陆','柒','捌','玖'] r2=['','拾','佰','仟','万','拾','佰','仟','亿''拾','佰','仟'] # 整数部分 tran_int='' for i in range(len(n_int)): if n_int[i]=='0': tran_int = r1[int(n_int[i])]+tran_int else: tran_int = r1[int(n_int[i])]+r2[i]+tran_int tran_int=tran_int.replace('壹拾','拾') # 若有多个'零'连续出现,则需要将其替换为一个'零' tem=re.findall(r'零{2,}',tran_int) #print(tem) if tem: # 如果tem不是空列表 for t in tem: tran_int=tran_int.replace(t,'零') if tran_int[-1]=='零': tran_int=tran_int[:-1] if tran_int!='': tran_int += '元' # 小数部分 if int(n_deci)==0: tran_int += '整' else: if n_deci[0]=='0': tran_int += r1[int(n_deci[1])]+'分' elif n_deci[1]=='0': tran_int += r1[int(n_deci[0])]+'角' else: tran_int += r1[int(n_deci[0])]+'角'+r1[int(n_deci[1])]+'分' tran_int='人民币'+tran_int print(tran_int) except: break