题解 | #人民币转换#
人民币转换
http://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
import sys z_dic = {'0':'零', '1':'壹','2':'贰','3':'叁','4':'肆','5':'伍','6':'陆','7':'柒','8':'捌','9':'玖','10':'拾' ,'100':'佰','1000':'仟','10000':'万','100000000':'亿'} def get_z(a_s): re = '' a_s = a_s.rjust(4, '0') if a_s[0] == '0': re += z_dic[a_s[0]] else: re += z_dic[a_s[0]] + z_dic['1000'] if a_s[1] == '0': if re[-1] != z_dic['0']: re += z_dic[a_s[1]] else: re += z_dic[a_s[1]] + z_dic['100'] if a_s[2] == '0': if re[-1] != z_dic['0']: re += z_dic[a_s[2]] else: if a_s[2] != '1': re += z_dic[a_s[2]] + z_dic['10'] else: re += z_dic['10'] if a_s[3] != '0': re += z_dic[a_s[3]] if len(re) > 1: re = re.rstrip('零') return re while True: try: a_s = input() x_s = "00" if '.' in a_s: i_s, x_s = a_s.split('.') else: i_s = a_s result = '人民币' if len(i_s) > 8: result += get_z(i_s[:-8]) + z_dic['100000000'] if len(i_s) > 4: result += get_z(i_s[:-4]) + z_dic['10000'] result += get_z(i_s[-4:]) result += "元" if result[3] == '零' and result[4] != '元': result = result.replace('零', '', 1) if i_s == '0': result = '人民币' x_s = x_s.rjust(2,'0') if x_s[:2] == "00": result += "整" else: if x_s[0] != '0': result += z_dic[x_s[0]] + '角' if x_s[1] != '0': result += z_dic[x_s[1]] + '分' print(result) except: # print(sys.exc_info()) break