c++ - Create the sums of all possible combinations of a vector -


i have been trying create program supposed find possible combinations of numbers in vector, , add them together, @ end check if match given number. not want find every number add specific number, rather sum of possible combinations. there many versions of question already, have managed make answers suit problem.

i have tried recursive version, one:

vector<int> set = {2,3,4,2}; vector<int> allpossible; vector<int> onepossible = {}; int sum = 0;  void recursive_comb(int step_val, int array_index) {     cout << "---" << endl;     (int = array_index; < set.size(); i++){         onepossible.push_back(set[i]);         sum = 0;         (int j = 0; j < onepossible.size(); j++){             sum += onepossible[j];             if (step_val < 0){                 onepossible = {};             }         }         cout << "adding " << sum << " allpossible" << endl;         allpossible.push_back(sum);         recursive_comb(step_val - 1, + 1);     } } 

and main function looks this:

int main(){     recursive_comb(set.size() - 1, 0);     (int = 0; < allpossible.size; i++){         (int j = 0; j < allpossible.size; j++){             if (allpossible[i] == allpossible[j]){                 cout << "there 2 possible combinations alike << endl;             }         }     } }  

my problem when onepossible emptied, doesn't empty @ all. have tried emptying @ every point in loop, doesn't work either, numbers added allpossible this:

[2], [2], [3], [4] //etc...... 

and want this:

[2], [2], [3], [4], [2+2=4], [2+3=5], [2+4=6], [3+4=7] //not including repeating ones might get. 

so actual question is:

how possible sums vector ([1], [2], [1] + [2], etc) ?

thank answer might provide!

from picklingtools distribution, there class compute combinations.

// compute combinations of n choose k.  computes n choose // k of combinations of distinct integers 1..n.  note // permutations permuted in sorted order. other data can // "combinated" using numbers here indicies array.  // // typical usage // #include "occombinations.h" {    combinations c(n,k);    {      int* cur = c.currentcombination();      printcurrentcombination(cur, n, k);    }  while (c.nextcombination()); } 

what returns list of indices can use permute vector:

  combinations c(n,k);   {      int *cur = c.currentcombination();        int sum = 0;      (int ii=0; ii<k; ii++) {         sum += vec[cur[ii]-1];  // notice indirection      }      if (sum==match_sum) { .. .. }   } while (c.nextcombination()); 

the combinations class gives 1 based combinations, notice -1. 5 choose 2 combinations give are:

n = 5 k = 2 (1 2) (1 3) (1 4) (1 5) (2 3) (2 4) (2 5) (3 4) (3 5) (4 5) 

edit: combinations generator modelled after permutations generator c++ stl. actual code file combinations standalone: pull out "occombinations.h" out of picklingtools distribution (it's under c++/opencontainers_1_8_2/include) , use without needing whole distribution.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -