写一个程序来赚钱

时间:2017-10-22 20:40:01

标签: python

我想编写一个程序,为我提供最低数量的可以改变LBP的账单。

import sys
n = int(sys.argv[1])

if n%100==0:
   print(n//100, " LBP ", 10**5)

elif n//100!=0 and n%50==0:
     print(n//100, " LBP ", 10**5)
     m = n-100*(n//100)
     print(m//50, " LBP ", 5*(10**4))

elif n//100!=0 or n//50!=0 and n%20==0:
     print(n//100, " LBP ", 10**5)
     m = n-100*(n//100)
     print(m//50, " LBP ", 5*(10**4))
     o = m - 50*(m//50)
     print(o//20, " LBP ", 2*(10**4))

elif n//100!=0 or n//50!=0 or n//20!=0 and n%10==0:
     print(n//100, " LBP ", 10**5)
     m = n-100*(n//100)
     print(m//50, " LBP ", 5*(10**4))
     o = m - 50*(m//50)
     print(o//20, " LBP ", 2*(10**4))
     p = o - 20*(o//20)
     print(p//10, " LBP ", (10**4))

elif n//100!=0 or n//50!=0 or n//20!=0 or n//10!=0 and n%5==0:
     print(n//100, " LBP ", 10**5)
     m = n-100*(n//100)
     print(m//50, " LBP ", 5*(10**4))
     o = m - 50*(m//50)
     print(o//20, " LBP ", 2*(10**4))
     p = o - 20*(o//20)
     print(p//10, " LBP ", (10**4))
     q = p - 10*(p//10)
     print(n//5, " LBP ", 5*(10**3))

elif n//100!=0 or n//50!=0 or n//20!=0 or n//10!=0 or n//5!=0 and n%1==0:
     print(n//100, " LBP ", 10**5)
     m = n-100*(n//100)
     print(m//50, " LBP ", 5*(10**4))
     o = m - 50*(m//50)
     print(o//20, " LBP ", 2*(10**4))
     p = o - 20*(o//20)
     print(p//10, " LBP ", (10**4))
     q = p - 10*(p//10)
     print(n//5, " LBP ", 5*(10**3))
     r = q - 5*(q//10)
     print(n-m-o-p-q-r, " LBP ", 10**3)

它对一些数字不起作用,例如134。 你可以帮我解决一下或建议其他任何方式来写它吗?

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

这应该有效:

def lbp(num):
    count = {}
    for bill in [100, 50, 20, 10, 5, 1]:
        count[bill] = num // bill
        num %= bill
    return {k: v for k, v in count.items() if v > 0}


import sys
n = int(sys.argv[1])

for bill, count in sorted(lbp(n).items(), reverse=True):
    print(count, ' LBP ', bill*1000)

使用循环和函数可以将其清理干净。