博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Loading Cargo
阅读量:5074 次
发布时间:2019-06-12

本文共 1987 字,大约阅读时间需要 6 分钟。

Loading Cargo

"Look Stephen, here's a list of the items that need to be loaded onto the ship. We're going to need a lot of batteries." Nikola handed him a notepad.

"What are the numbers next to the items?"

"That is the weight of each item."

"Er, why?"

"So you can see how much your trading cards and comic book collection will weigh us down during flight." Rang Sofias voice from the phone tube.

"What is she talking about?” asked Nikola “Ooooh, nevermind, that was sarcasm. It’s important because your load-stabilizing belt is broken and there is no way that we can find a new one right now. That’s why, when you carry the things on the list, you’ll have to redistribute their weights in order to get the minimal weight in each arm."

"Okay, so I have to figure out how many batteries I should hold in each hand to prevent them from breaking when they inevitably fall to the ground. Got it."

You have been given a list of integer weights. You should help Stephen distribute these weights into two sets, such that the difference between the total weight of each set is as low as possible.

Input data: A list of the weights as a list of integers.

Output data: The number representing the lowest possible weight difference as a positive integer.

原题链接: 

题目大意: 将数组中的数分为两组, 使得两组和之差最小

思路: 暴力法, 计算出所有分组和, 求最小差值; 时间复杂度N^2

1 def checkio(data): 2     #cal all possible subset sum 3     minmum_gap = 100 4     sub_sum = {0} 5     total_sum = sum(data) 6  7     for each in data: 8         tmp_list = [] 9         10         for pre in sub_sum:11             tmp_sum = each + pre12 13             if tmp_sum not in sub_sum:14                 tmp_list.append(tmp_sum)15 16                 tmp_gap = abs((tmp_sum << 1) - total_sum)17 18                 if tmp_gap < minmum_gap:19                     minmum_gap = tmp_gap20 21         for new_sum in tmp_list:22             sub_sum.add(new_sum)23 24     #replace this for solution25     return minmum_gap

 

转载于:https://www.cnblogs.com/hzhesi/p/3935428.html

你可能感兴趣的文章
JavaScript escape encodeURI
查看>>
使用javascript模拟常见数据结构(一)
查看>>
hdu 5514 容斥原理
查看>>
golang 报错信息及解决方法--采坑之路,学习使我快乐
查看>>
go for-range中的循环变量
查看>>
键值的转换
查看>>
Android环境开发搭建
查看>>
POJ 1664 放苹果
查看>>
用户交互程序
查看>>
github pages & 在线预览
查看>>
Jenkins+Jmeter持续集成笔记(五:问题优化)
查看>>
摘录:Jetty 的工作原理以及与 Tomcat 的比较
查看>>
stringstream 与空格 (大家讨论一下代码结果的原因)
查看>>
词性标注 parts of speech tagging
查看>>
git 入门(转)
查看>>
三、windows8 store
查看>>
Jenkins自动构建的几种方式
查看>>
MyEclipse 启动 tomcate 失败 解决方法
查看>>
[ SCOI 2005 ] 最大子矩阵
查看>>
[ NOI 2002 ] 荒岛野人
查看>>