Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e002075

Browse files
Update 背包问题理论基础完全背包.md
1 parent b9faa11 commit e002075

File tree

1 file changed

+56
-21
lines changed

1 file changed

+56
-21
lines changed

‎problems/背包问题理论基础完全背包.md‎

Lines changed: 56 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -222,44 +222,79 @@ private static void testCompletePackAnotherWay(){
222222

223223

224224
Python:
225-
225+
先遍历物品,再遍历背包(无参版)
226226
```python
227-
# 先遍历物品,再遍历背包
228-
def test_complete_pack1():
227+
def test_CompletePack():
229228
weight = [1, 3, 4]
230229
value = [15, 20, 30]
231-
bag_weight = 4
230+
bagWeight = 4
231+
dp = [0] * (bagWeight + 1)
232+
for i in range(len(weight)): # 遍历物品
233+
for j in range(weight[i], bagWeight + 1): # 遍历背包容量
234+
dp[j] = max(dp[j], dp[j - weight[i]] + value[i])
235+
print(dp[bagWeight])
232236

233-
dp = [0]*(bag_weight +1)
237+
test_CompletePack()
234238

235-
for i in range(len(weight)):
236-
for j in range(weight[i], bag_weight + 1):
239+
```
240+
241+
先遍历物品,再遍历背包(有参版)
242+
```python
243+
def test_CompletePack(weight, value, bagWeight):
244+
dp = [0] * (bagWeight + 1)
245+
for i in range(len(weight)): # 遍历物品
246+
for j in range(weight[i], bagWeight + 1): # 遍历背包容量
237247
dp[j] = max(dp[j], dp[j - weight[i]] + value[i])
238-
239-
print(dp[bag_weight])
248+
return dp[bagWeight]
240249

241-
# 先遍历背包,再遍历物品
242-
def test_complete_pack2():
250+
if __name__ == "__main__":
243251
weight = [1, 3, 4]
244252
value = [15, 20, 30]
245-
bag_weight = 4
253+
bagWeight = 4
254+
result = test_CompletePack(weight, value, bagWeight)
255+
print(result)
246256

247-
dp = [0]*(bag_weight + 1)
257+
```
258+
先遍历背包,再遍历物品(无参版)
259+
```python
260+
def test_CompletePack():
261+
weight = [1, 3, 4]
262+
value = [15, 20, 30]
263+
bagWeight = 4
248264

249-
for j in range(bag_weight + 1):
250-
for i in range(len(weight)):
251-
if j >= weight[i]: dp[j] = max(dp[j], dp[j - weight[i]] + value[i])
252-
253-
print(dp[bag_weight])
265+
dp = [0] * (bagWeight + 1)
266+
267+
for j in range(bagWeight + 1): # 遍历背包容量
268+
for i in range(len(weight)): # 遍历物品
269+
if j - weight[i] >= 0:
270+
dp[j] = max(dp[j], dp[j - weight[i]] + value[i])
271+
272+
print(dp[bagWeight])
273+
274+
test_CompletePack()
254275

255276

256-
if __name__ == '__main__':
257-
test_complete_pack1()
258-
test_complete_pack2()
259277
```
260278

279+
先遍历背包,再遍历物品(有参版)
280+
```python
281+
def test_CompletePack(weight, value, bagWeight):
282+
dp = [0] * (bagWeight + 1)
283+
for j in range(bagWeight + 1): # 遍历背包容量
284+
for i in range(len(weight)): # 遍历物品
285+
if j - weight[i] >= 0:
286+
dp[j] = max(dp[j], dp[j - weight[i]] + value[i])
287+
return dp[bagWeight]
288+
261289

290+
if __name__ == "__main__":
291+
weight = [1, 3, 4]
292+
value = [15, 20, 30]
293+
bagWeight = 4
294+
result = test_CompletePack(weight, value, bagWeight)
295+
print(result)
262296

297+
```
263298

264299
Go:
265300
```go

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /