lundi 18 décembre 2017

Trying to print out special type of combination that equal to a sum value

Hi i need a little help i found this code below from the internet i like to add some changes to this code .

This code print out all possible combination of the slots total value must be = sum
How can i control each slot with a maximum capacity value Example if the slots = 3,and sum= 8 then i like to add a capacity (array or vector) = [4][2][3] all the sequence must be inside this range . so i can also skip sequence like this that equal to sum [0][4][4] . i need help sorry for my English l am learning ..

#include <iostream> 
#include <vector> 
#include <algorithm> 

template <typename T, typename ForwardIterator> 
bool increment(ForwardIterator first, ForwardIterator last, T maximum) { 
  for (auto it = first; it != last; ++it) { 
      if (*it != maximum) { 
       std::fill(first, it, ++*it); 
      return true; 
    } 
   } 
   return false; 
 } 

int main() 
{ 
 int minimum = 1; // included 
 int slots = 3; 
 int sum = 8; 
 int internal_max = sum - slots * minimum; 
 std::vector<int> vect(slots - 1, 0); 
 do { 
   auto previous_pos = internal_max; 
    for (auto it = vect.begin(); it != vect.end(); ++it) { 
     auto val = previous_pos - *it + minimum; 
     previous_pos = *it; 
     std::cout << val << " "; 
   } 
   std::cout << previous_pos + minimum << std::endl; 
 } while (increment(vect.begin(), vect.end(), internal_max)); 
} 

Aucun commentaire:

Enregistrer un commentaire