开发者

Least brute force way to see how many groups of numbers in a group of numbers add to a given number in C++ or Python

The title is probably a little ambiguous.

There is a given number.

There is a set of numbers all less than the given number and greater than zero.

I want to know how many different combinations of adding numbers there are that equal the given number.

For example if the given number is 14 and the numbers in the set are 7, 7, 7, 6, 4, and 4. There would be 4 combinations. (7+7, 7+7, 7+7, 6+4+4)

I don't really care what language the so开发者_运维知识库lution is in, but I would prefer C++ or Python.


Don't be discouraged by the claims that this is NP-complete. The Wikipedia article on Subset Sum linked by @isbadawi mentions a 1999 paper by Professor David Pisinger with an algorithm that sounds like it would work with your existing constraints in linear time: http://www.sciencedirect.com/science/article/pii/S0196677499910349 . It's $31.50, but that sounds like a bargain if you really need to solve this problem well.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜