简体   繁体   中英

Finding all possible sums of a set

I am currently looking for ideas on how to find all possible sums of a set of numbers with these rules. I have these numbers to work with and I want to find all possible sums so that you can only use a single number at max 4 times and each time you pick 7 of these numbers.

{ 0, 1, 5, 22, 98, 453, 2031, 8698, 22854, 83661, 262349, 636345 and 1479181 }

Acceptable examples would be

0 + 0 + 0 + 0 + 83661 + 83661 + 2031

Unacceptable example would be

0 + 0 + 0 + 0 + 0 + 83661 + 2031

The only way I can think of is a series of nested loops but I am having trouble with that as well. Would there be any other options to do this. I am using Java but I don't really think that matters.

You can achieve this by building a new List of elements that contains each element of the given set duplicated 4 times . then use a DFS strategy method to build possible possible sum combination. to have an idea about how to implement DFS check this answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM