Skip to content

Commit cc0f6e7

Browse files
committed
11
1 parent f5eeca9 commit cc0f6e7

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

finalPackage.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
maxWight =eval(input("输入背包容量"))
2+
weight = eval(input("依次输入商品重量"))
3+
value = eval(input("依次输入商品价格"))
4+
arr = [(i, value[i]/weight[i], weight[i], value[i])
5+
for i in range(len(weight))]
6+
7+
8+
arr.sort(key=lambda x: x[1], reverse=True)
9+
bagVal = 0
10+
bagList = []
11+
costList = []
12+
for i, per, weight, value in arr:
13+
if weight <= maxWight:
14+
maxWight -= weight
15+
bagVal += value
16+
bagList.append(i)
17+
costList.append(weight)
18+
else:
19+
bagVal += maxWight*per
20+
bagList.append(i)
21+
costList.append(maxWight)
22+
break
23+
24+
25+
print('\n排序后:', arr)
26+
print('能运走的最大价值:%.2f' % bagVal)
27+
print('此时承载的宝物有:', bagList)
28+
print('货物依次取出:', costList)

可拆分背包问题.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
### 可拆分背包问题
2-
2+
### xxxx
33
~~~
44
maxWight = 20
55
weight = (5, 15, 7)

0 commit comments

Comments
 (0)