def changeMaking(changeList, target):
changeLength = len(changeList)
F = [0]*changeLength
for i in range(1,target):
minimum = 99999
j = 1
for j in range(1, changeLength):
if changeList[j] <= i:
if 1+F[i-changeList[j] < minimum:
minimum = 1 + F[i-changeList[j]]
coin = j
return F[i] = minimum
def main():
changeList = [1, 5, 10, 15, 25] #coin denominations
target = 150
result = changeMaking(changeList, target)
print(result)
main()
我正在尝试制作一个更改程序,但我无法弄清楚如何使这个算法工作。问题应该是显而易见的......它不起作用。算法尝试做的是找到找到目标金额所需的最低金币数量。
答案 0 :(得分:1)
这对你有什么用?
def changeMaking(list, amount, check=False):
list = reversed(sorted(list))
result = []
changeCount = 0
for coin in list:
count = 0
while changeCount + coin <= amount:
changeCount = changeCount + coin
count = count + 1
if count > 0:
result.append([coin, count])
if not check or changeCount == amount:
return result
else:
return False
你没有向我们提供太多信息,所以我认为你会希望它尽可能少地返回它。
它的工作方式如此
change = changeMaking(coins, amount)
例如
change = changeMaking([1, 2, 5, 10, 20, 50, 100], 56)
返回
[[50, 1], [5, 1], [1, 1]]
它还接受第三个参数,当该值为true时,该函数会检查是否可以进行更改(如果您的硬币列表中有1,则该值应始终为true)。