python 动态规划
程序员文章站
2022-05-05 17:37:26
...
背包可容纳6kg
水1kg,10刀
书1kg,3刀
食物2kg,9刀
夹克2kg,5刀
照相机1kg,6刀
求能装下且总价值最大。
A = [[0 for i in range(6)] for j in range(5)]
W = {}
W["book"] = [1, 3, 1]
W["food"] = [2, 9, 2]
W["jacket"] = [2, 5, 3]
W["camer"] = [1, 6, 4]
t = 0
for i in range(6):
if (i + 1 >= 3):
A[0][i] = 10
for n, m in W.items():
if ((i + 1 - m[0]) >= 0):
A[m[2]][i] = max(A[m[2] - 1][i], A[m[2] - 1][i - m[0]] + m[1])
else:
A[m[2]][i] = A[m[2] - 1][i - 1]
print(max(max(A)))
上一篇: python 递归+快排
下一篇: 3.28