这个任务对我来说确实很难。 我的任务是找出Sarah可以达到的最高幸福,因为她的包包容量和可供出售的物品。 莎拉携带的包最多可携带3件物品,总重量不超过w kg。 Sarah购买的所有物品必须同时装入她的包里。 例如: w = 97 items = [[17, 19], [22, 19], [65, 64], [21, 19], [27, 26], [19, 21], [58, 61], [43, 46], [1, 3], [44, 42], [22, 22], [52, 53], [10, 8], [37, 35], [60, 62], [42, 39], [36, 36], [62, 60], [50, 47], [62, 62], [47, 48], [15, 16], [12, 12], [6, 5], [30, 27], [52, 49], [30, 32], [3, 4], [21, 18], [58, 58], [43, 42], [50, 50], [41, 41], [60, 60], [55, 58], [64, 63], [32, 33], [11, 12], [11, 13], [46, 44], [22, 21], [20, 19], [47, 45], [24, 24], [35, 32], [28, 31], [14, 15], [35, 37], [9, 10], [23, 22], [45, 46], [34, 32], [34, 37], [37, 39], [42, 41], [59, 57], [24, 22], [15, 13], [33, 34], [3, 3], [55, 55]]
第一个数字是物品的重量。 第二个数字是项目的幸福值。 任何人有任何想法如何计算?Python:查找数组总和
-7
A
回答
-3
我得到105幸福,物品[11, 13], [28, 31], [58, 61]
和[28, 31], [34, 37], [35, 37]
使用所有可能的97重量和所有3个累积插槽。
# this allows for integers to be divided by integers and give a float result
# program is runnable in both python 2 and 3
from __future__ import division
from sys import exit
weight = 97
items = [[17, 19], [22, 19], [65, 64], [21, 19], [27, 26], [19, 21], [58, 61],
[43, 46], [1, 3], [44, 42], [22, 22], [52, 53], [10, 8], [37, 35], [60, 62],
[42, 39], [36, 36], [62, 60], [50, 47], [62, 62], [47, 48], [15, 16], [12, 12],
[6, 5], [30, 27], [52, 49], [30, 32], [3, 4], [21, 18], [58, 58], [43, 42],
[50, 50], [41, 41], [60, 60], [55, 58], [64, 63], [32, 33], [11, 12], [11, 13],
[46, 44], [22, 21], [20, 19], [47, 45], [24, 24], [35, 32], [28, 31], [14, 15],
[35, 37], [9, 10], [23, 22], [45, 46], [34, 32], [34, 37], [37, 39], [42, 41],
[59, 57], [24, 22], [15, 13], [33, 34], [3, 3], [55, 55]]
# this is about half of the problem: we must sort the items by the priority
# which which we want them. this would be their happiness to weight ratio
sorted_items = sorted(items, key=lambda item: item[1]/item[0], reverse=True)
happiness = int()
for item1 in range(len(sorted_items) - 2):
for item3 in range(item1 + 2, len(sorted_items)):
for item2 in range(item1 + 1, item3):
# try to use as much weight as possible, we don't want
# our bag to go underfilled with 3 efficient but light items
if (sorted_items[item1][0] +
sorted_items[item2][0] +
sorted_items[item3][0] <= weight):
# we haven't gone over weight! possible solution found!
happiness = max(sorted_items[item1][1] +
sorted_items[item2][1] +
sorted_items[item3][1], happiness)
print(happiness)
1
这是0/1 knapsack problem。最优雅的解决方案是动态编程。你可以找到关于它的讲座here,以及解决方案here。
我建议看看演讲,并尝试在查看解决方案之前自行编码。
相关问题
- 1. 在数组中查找计数总和
- 2. 循环查找数组java的总和
- 3. 查找总和为0的数组n
- 4. 查找数组,其总和使用PHP
- 5. 查找嵌套数组的总和
- 6. 查找数组中的组合总数?
- 7. python函数来找到数组中的正数的总和
- 8. 查找数组中的列总数
- 9. HIVE查询数组总和
- 10. 查找负数和总数的正数
- 11. 查找指数和总和名单
- 12. 拆分数字串和查找总和
- 13. 查找数组中的3个连续数字的总和
- 14. 查找数组中所有数字的排列总和为16
- 15. 在Ruby中查找数组中两个数字的总和
- 16. 如何查找数组中所有数字的总和?
- 17. Python的组和总和
- 18. 查找数组中的变量,哪个总和最接近目标总和
- 19. 柱找到分组数据的总和
- 20. 使用MIPS找到数组的总和
- 21. 找到二维数组的总和java
- 22. 使用组中的项目总数和数量在组中查找数字
- 23. 查找总和小于给定数组的数组的最大数目
- 24. 查找数组,等于剩余的元素的总和元素
- 25. 查找与给定总和相等的数组元素
- 26. 查找管理对象数组的总和
- 27. 查找Session数组中所有键的总和
- 28. 使用lodash或underscorejs查找数组字段总和
- 29. 查找特定多维数组的总和php
- 30. 如何查找JTable中二维数组中每行的总和
考虑到这是功课,你不应该真的试图弄清楚,并在过程中学习的东西? – isedev 2014-09-27 07:32:26
@isedev这太苛刻了:也许Pheng-Khai Tan有一个叫Sarah的朋友,他带着一个可以容纳最多三件总重量* w * kg的袋子,并带有一个易于测量的快乐值。 – Veedrac 2014-09-27 07:36:27
@ Pheng-KhaiTan我建议买一个背包。但严重的是,我建议你阅读[我如何问及回答作业问题?](http://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions)。 – Veedrac 2014-09-27 07:41:56