题目:52. 携带研究材料
我的感悟:
- 背模板,记下来,就好了。
理解难点:
听课笔记:
代码示例:
n, v = map(int, input().split())
weight_list = []
value_list = []
for _ in range(n):
weight, value = map(int, input().split())
weight_list.append(weight)
value_list.append(value)
# print(n,v)
# print(weight_list, value_list)
def func(weight_list, value_list, bag_size):
dp = [0] * (bag_size + 1)
for i in range(len(weight_list)): # 遍历物品
for j in range(weight_list[i],bag_size + 1): # 正序遍历背包
dp[j] = max(dp[j], dp[j-weight_list[i]]+value_list[i])
# print(dp)
return dp[bag_size]
print(func(weight_list,value_list,v))
通过截图:
扩展写法:
资料:
完全背包
视频讲解:https://www.bilibili.com/video/BV1uK411o7c9
标签:weight,8C%,E5%,随想录,list,value,52,part6,dp From: https://www.cnblogs.com/liqi175/p/18039917