从数字集中获取给定数字的最小上界的逻辑形式
我的问题如下-
我有一些数字,如下所示-
2
2
2
2
3
3
17
17
17
17
17
17
17
17
17
34
34
34
34
34
68
68
68
136
所以如果我给出以下数字作为输入,输出应如下-
[输出是给定数字的总和, 刚刚大于输入]
Input Output
3 2,2
4 2,2
254 17,34,68,136
7 2,3,3 [or also with 2,2,2,2 but if return same sum,
then number count should min]
205 2,68,136
10 2,2,3,3
我不想尝试每一个组合(即暴力)来获得结果。所以想问一下有没有什么有效的算法可以解决上述情况。
谢谢。
My problem is as follows-
I have some numbers with me, like below-
2
2
2
2
3
3
17
17
17
17
17
17
17
17
17
34
34
34
34
34
68
68
68
136
So if I give the following number as input, the output should be as follows-
[output is the sum of given number,
that just greater than the input]
Input Output
3 2,2
4 2,2
254 17,34,68,136
7 2,3,3 [or also with 2,2,2,2 but if return same sum,
then number count should min]
205 2,68,136
10 2,2,3,3
I don't just want to try each and every combination (i.e brute force) to get the result. So just want to ask is there any efficient algorithm possible for the above situation.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在维基百科上找到了一个可能的起点:
您的基本问题看起来像是该问题的扩展版本。您需要找到集合的一个子集,求和为
input
- 或者失败,求和为input+1
,或者失败,求和为input+2< /code> 等。
所以只要重复运行 Pisinger 算法,增加目标总和,直到得到结果? (我没有读过论文,所以不知道Pisinger算法是否满足选择最小子集的决胜条件。)
I found a possible starting point on Wikipedia:
Your basic problem looks like an extended version of that. You need to find a subset of your set summing to
input
- or failing that, summing toinput+1
, or failing that, summing toinput+2
, etc.So just run Pisinger's algorithm repeatedly, with increasing target sums, until you get a result? (I haven't read the paper, so I don't know whether the tiebreaker condition of choosing the smallest subset is satisfied by Pisinger's algorithm.)