python 找零钱方案

python:找零钱方案

def zhaoling(price,pay):
    re=float(pay)-float(price)
    if re==0:
        return 0
    coins=[50,10,5,1]
    decoin=[0.5,0.2,0.1]
    
    restr=str(re)
    integer=int(restr.split(".")[0])
    decimal=round(float(restr.split(".")[1])*0.1,1)
    redict={}
    for i in coins:
        n=integer//i
        
        if n!=0:
            redict[i]=n;
        integer=integer%i
        if integer==0:
            break;
            
    for i in decoin:
        n=decimal//i
        if n!=0:
            redict[i]=int(n);
        decimal=decimal%i
        if decimal==0:
            break;
    
    ret=""
    for i in redict:
        ret=ret+str(i)+":"+str(redict[i])+";"

    print(str(re)+" "+ret)    

def main():
    x =input()
    a=x.split(" ")
    zhaoling(a[0],a[1])
    
if __name__ =='__main__':
    
    main()

输入:

26.5 50

输出:

23.5 10:2;1:3;0.5:1;

输入:

27 50

输出:

23.0 10:2;1:3;
posted @ 2020-10-25 16:14  落地成霜  阅读(1139)  评论(0编辑  收藏  举报