我正在尝试编写硬币更改功能。我的想法是,我有无限的面额和任何价值,并试图看到我能得到什么样的改变。我认为这可以像一个无限的背包程序,但我得到一个奇怪的输出,我不明白这意味着什么。我想也许是因为我输入了值和小数,并且想要将函数中的值更改为float但是这给了我一个错误并且不会运行程序。有人可以解释我目前的输出说的是什么吗?是否有可能用小数做这种事情?我怎样才能改善这个?
#change_tuple is the type of coin and the value of the coin
#change_amount is how much change to get
def make_change(coin_tuples, change_amount):
#max_change = change_amount
#max_change = [0]*(change_amount)
max_change = [0]*(change_amount + 1)
for current_total in range(change_amount + 1):
total_change = 0
for coin_type, coin_value in coin_tuples:
if (coin_type <= current_total):
max_change_using_coin = coin_value + max_change[current_total - coin_type]
total_change = max(max_change_using_coin, total_change)
max_change[current_total] = total_change
return max_change
coin_tuples = ([1, .01], [5, .05], [10, .1], [25, .25], [50, .5], [100, 1])
change_amount = 20
make_change(coin_tuples, change_amount)
输出:[0, 0.01, 0.02, 0.03, 0.04, 0.05, 0.060000000000000005, 0.07, 0.08, 0.09, 0.1, 0.11000000000000001, 0.12000000000000001, 0.13, 0.14, 0.15000000000000002, 0.16000000000000003, 0.17000000000000004, 0.18000000000000005, 0.19000000000000006, 0.20000000000000007]