题解 | #人民币转换#

人民币转换

https://www.nowcoder.com/practice/00ffd656b9604d1998e966d555005a4b

我的方法比较笨:写了五个函数,分别处理100以内的一个函数,100-1000的一个函数,1000-10000一个函数,大于10000的再一个函数,然后数字更的函数调用更小的函数。
#-*-coding:utf-8-*-
x,y = map(int,raw_input().split('.'))
dic = {1:'壹', 2:'贰', 3:'叁', 4:'肆', 5:'伍', 6:'陆', 7:'柒', 8:'捌', 9:'玖', 10:'拾', 11:'拾壹', 12:'拾贰', 13:'拾叁', 14:'拾肆', 15:'拾伍', 16:'拾陆', 17:'拾柒', 18:'拾捌', 19:'拾玖'}
res = ["人民币"]
###处理小数
def ny(y):
    sy = str(y)
    if y>=10:
        if sy[0]!='0' and sy[1]=='0':
            res.append(dic[int(sy[0])]+"角")
        elif sy[0]!=0 and sy[1]!=0:
            res.append(dic[int(sy[0])]+"角"+dic[int(sy[1])]+"分")
    else:
        res.append(dic[int(sy[0])]+"分")
##处理小于100的证书部分        
def n2w(x):
    sx = str(x)
    if x<20:
        res.append(dic[x])
    else:
        res.append(dic[int(sx[0])]+'拾'+dic[int(sx[1])])

#处理整数部分数字的百位
def n3w(x):
    sx = str(x)
    if x>=100:
        res.append(dic[int(sx[0])]+'佰')
        if sx[1]=='0':
            res.append("零")
        n2w(int(sx[1:]))
    else:
        n2w(x)
##处理整数部分数字千位
# def n4w(x):
    sx = str(x)
    if x>=1000:
        res.append(dic[int(sx[0])]+'仟')
        if sx[1]=='0':
            res.append("零")
        n3w(int(sx[1:]))
    else:
        n3w(x)
##处理整数部分数字的万位到亿位之间的数
def n5w(x):
    sx = str(x)
    if x>=10000:
        n4w(int(sx[0:-4]))
        res.append('万')
        if sx[-4] == '0':
            res.append("零") 
        n4w(int(sx[-4:]))
    else:
        n4w(x)
##处理整数部分数字的亿位以上的数        
def n9w(x):
    sx = str(x)
    if x >= 100000000:
        n5w(int(sx[0:-8]))
        res.append('亿') 
        n5w(int(sx[-8:]))
    else:
        n5w(x)

if x >0 and y != 0:
    n9w(x)
    res.append('元')
    ny(y)
elif x>0 and y==0:
    n9w(x)
    res.append('元整')
else:
    ny(y)
print ''.join(res) 


全部评论

相关推荐

被加薪的哈里很优秀:应该继续招人,不会给你留岗位的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务