forked from shibing624/python-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreedy.py
More file actions
29 lines (28 loc) · 1.02 KB
/
greedy.py
File metadata and controls
29 lines (28 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# -*- coding: utf-8 -*-
"""
@author:XuMing([email protected])
@description:
"""
# coding=utf-8
if __name__ == '__main__':
beg = 50 #背包50kg
value = 0 #已经获得的价值
choice = []
while beg > 0: #如果背包还有空位,则递归
if beg >= 8: #选择当前这一步的最优解,既选择B商品
beg = beg - 8
value = value + 13
choice.append("B")
elif beg >= 10: #要是B商品选择不了,则选择第二单位价值的物品,即A物品
beg = beg - 10
value = value + 15
choice.append("A")
elif beg >= 6:
beg = beg - 6
value = value + 8
choice.append("C")
else: #当所有的物品都选择不了,则退出
break
print("剩余的背包重量:",beg)
print("获得的总价值:",value)
print("选择的物品的类型及顺序:",choice)