ian-witz, , , NP- : , , . , , , , , , .
, , :
Set up a place to hold your results. The results will all be lists of numbers, each some sub-set of your original list. We don't know how many such lists will result; possibly none.
Put your list of numbers into an array so you can refer to them and access them by index. In the following, I'm assuming array indices starting at 1. Say you have 10 numbers, so you put them into a 10 element array, indexed by positions 1 through 10.
For performance reasons, it may help to sort your array in descending order. It not necessary though.
Run a first index, call it i, through this array, i.e. through index values 1 through 10.
For each index value:
select the number at index position i, call it n1.
set up a new list of numbers, where we will be assembling a sub-list. call it sublist.
add n1 to the (so far empty) sublist.
If i is already at 10, there nothing more we can do. Otherwise,
Run a second index, call it j, through the arrray, starting at i+1 and going up to 10.
For each value of j:
select the number at index position j, call it n2.
add n2 to the sublist containing n1
calculate the sum of our sublist so far: Does it add up to 18000?
If the exact total is reached, add the current sublist to our result list.
If the total is exceeded, there nothing we can add to make it better, so skip to the next value of j.
If the total is less than 18000, you need to pick a third number.
Run a third index, call it k, through the array, starting at j+1 and going up to 10. Skip this if j is already at 10 and there no place to go.
For each value of k:
select the number at k, call it n3
add n3 to the sublist
check the sublist total against the expected total
if the exact total is reached, store the sublist as a result;
if it less than the expected, start a 4th loop, and so on.
When you're done with checking a value for a loop, e.g. n4, you need to take your latest n4, n3 or whatever back out of the sublist because you'll be trying a different number next.
Whenever you find a combination of numbers with the correct sum, store it in your results set.
When you've run all your loop counters into the wall (i.e. i is 10 and there nowhere left to go), your "results" set will contain all sub-lists of the original list that added up to the desired total. It possible there will be none, in that case there no (exact) solution to your problem.
If you have 3 or more sub-lists in your results set, you can check if you can find a pair of them that use non-overlapping sets of numbers from the original list. If you have 2, then there should also be a 3rd sub-list containing exactly all the numbers not contained in the first 2 lists, and you have your solution.
; , 1 () 10 18000. , , 2000, , 2 (= + 1) 16000. ( + 1) (16000 - ), . "" - , .
How to implement this effectively depends on the language in which you make it. FORTRAN 77 does not have recursion, Lua does not implement lists or sets efficiently, Lisp may have problems with efficient indexing to a list. In Java, I could use a bit set rather than a sublist. I don’t know anything about P4GL, therefore: for implementation you are on your own!