该算法可能对您有所帮助!
def greedy(amount, denoms):
result = []
while (amount > 0):
print amount, denoms, result
if (amount >= denoms[0]):
num = amount // denoms[0]
amount -= (num * denoms[0])
result.append([denoms[0], num])
denoms = denoms[1:]
return result
print greedy(100, [25,10,5,1])
print ""
print greedy(100, [10,5,1])
print ""
print greedy(100, [5,1])
print ""
print greedy(100, [1])
print ""
print greedy(47, [25,10,5,1])
输出将是
100 [25, 10, 5, 1] []
[[25, 4]]
100 [10, 5, 1] []
[[10, 10]]
100 [5, 1] []
[[5, 20]]
100 [1] []
[[1, 100]]
47 [25, 10, 5, 1] []
22 [10, 5, 1] [[25, 1]]
2 [5, 1] [[25, 1], [10, 2]]
2 [1] [[25, 1], [10, 2]]
[[25, 1], [10, 2], [1, 2]]