题解 | #人民币转换#
人民币转换
http://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b
各种if语句应接不暇
x = '零、壹、贰、叁、肆、伍、陆、柒、捌、玖' xi = '壹、贰、叁、肆、伍、陆、柒、捌、玖' xi = xi.split('、') y = {} for n, i in enumerate(x.split('、')): y[str(n)] = i ii = '123' def get_bai(ii): if len(ii) == 1: u = y[ii] elif len(ii) == 2: u = y[ii[0]]+'拾'+y[ii[1]] elif len(ii) == 3: u = y[ii[0]]+'佰'+y[ii[1]]+'拾'+y[ii[2]] elif len(ii) == 4: u = y[ii[0]]+'仟'+y[ii[1]]+'佰'+y[ii[2]]+'拾'+y[ii[3]] else: return '零' ul = list(u) def get_l(ul): for n, i in enumerate(ul): if n+1 == len(ul): # 如果零后面没有了 则删除自己。 ul.pop(n) return ul elif i == '零' and ul[n+1] not in xi: # 如果零后面有单位或零,则删除后面的。 ul.pop(n+1) return get_l(ul) return ul if '零' in ul: get_l(ul) return ''.join(ul) def get_jf(ii): res = y[ii[0]]+'角'+y[ii[1]]+'分' if ii[1] == '0': res = res[0:-2] elif ii[0] == '0': res = res[-2:] return res def get_all(ii): if len(ii) <= 4: res = get_bai(ii) if len(res) == 3 and res[0] == '壹': res = res[1:] return res elif 4 < len(ii) < 9: res1 = get_bai(ii[0:-4]) if len(res1) == 3 and res1[0] == '壹': res1 = res1[1:] res2 = get_bai(ii[-4:]) return res1+'万'+res2 while True: try: l = list(map(str, input().split('.'))) if len(l) == 1: res = get_all(l[0]) print('人民币'+res+'元整') else: res = get_all(l[0]) if res: print('人民币'+res+'元' + get_jf(l[1]) ) else: print('人民币' +get_jf(l[1])) except EOFError: break