import sys
def minCoins(coins, m, how, V):
# table[i] will be storing the minimum
# number of coins required for i value.
# So table[V] will have result
table = [0 for i in range(V + 1)]
index = []
# Base case (If given value V is 0)
table[0] = 0
# Initialize all table values as Infinite
for i in range(1, V + 1):
table[i] = sys.maxsize
# Compute minimum coins required
# for all values from 1 to V
for i in range(1, V + 1):
# Go through all coins smaller than i
for j in range(m):
if (coins[j] <= i):
sub_res = table[i - coins[j]]
if (sub_res != sys.maxsize and
sub_res + 1 < table[i]):
if sub_res +1 <= how[j] :
table[i] = sub_res + 1
return table[V]
coins = [200, 100, 50, 20, 10, 5, 2, 1]
howmanycoins = [0, 2, 2, 3, 0, 0, 7, 8]
m = len(coins)
V = 16
print("Minimum coins required is ", minCoins(coins, m, howmanycoins, V))
我对此代码有疑问。
当表howmanycoins
具有此值时,[0, 2, 2, 3, 0, 0, 7, 9]
程序给出很好的答案7x“ 2” + 2x“ 1” = 9个硬币,但是当最后一个8位时,输出如下所示:
所需的最低硬币是9223372036854775807。