代码 :
def findMax(numOfItems,total_money,items,vlaues): #i是多少行 j是多少列
dp = [[0]*(total_money+1) for i in range(numOfItems+1)]
for i in range(1,numOfItems + 1):
for j in range(1,total_money + 1):
if j < items[i-1]: #只能放上一个物品,因为放不进比他大一个型号的
dp[i][j] = dp[i - 1][j]
else: #当可以放进时,就比较放进去的收益大还是不放的收益大
dp[i][j] = max(dp[i - 1][j],dp[i - 1][j - items[i-1]] + vlaues[i-1])
return dp[i][j]total_money = 1000
hotspot_val = [6, 10,3, 4, 5, 8]
unit_price = [200,600,100,180,300,450]
findMax(len(unit_price),total_money,unit_price,hotspot_val)