Форум » C/C++ для начинающих (C/C++ for beginners) » Find all unique combinations of numbers that have a sum that equals » Ответить

Find all unique combinations of numbers that have a sum that equals

ramees: [pre2] 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 = 17; 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)); }[/pre2] Above code print all combination without negative value and zeros But i need little change on this code. like this code output 15 , 1 , 1 = 17 .. so on i dont want any dual character or more in (15, 1 ,1 ) in this sequence 15 is dual character I ony want 1 row have value range between 1 to 9 For example the code output now 15 , 1 , 1 14 , 2 , 1 13 , 3 , 1 12 , 4 , 1 11 , 5 , 1 10 , 6 , 1 9 , 7 , 1 8 , 8 , 1 7 , 9 , 1 6 ,10 ,1 5 , 11 ,1 4 , 12 , 1 3 , 13 , 1 2 , 14 , 1 1 ,15 ,1 14 ,1 ,2 13 , 2 ,2 12, 3 ,2 11, 4 ,2 10 ,5 ,2 9 ,6 ,2 8 ,7 ,2 7, 8 ,2 6 ,9 ,2 5 ,10,2 4 ,11 ,2 3 ,12 ,2 2 ,13 ,2 1,14 ,2 3,1,3 12,2,3 11,3,3 10,4,3 9,5,3 8,6,3 7,7,3 6,8,3 5,9,3 4,10,3 3,11,3 2,12,3 1,13,3 12,1,4 11,2,4 10,3,4 9,4,4 8,5,4 7,6,4 6,7,4 5,8,4 4,9,4 3,10,4 2,11,4 1,12,4 11,1,5 10,2,5 9,3,5 8,4,5 7,5,5 6,6,5 5,7,5 4,8,5 3,9,5 2,10,5 1,11,5 10,1,6 9,2,6 8,3,6 7,4,6 6,5,6 5,6,6 4,7,6 3,8,6 2,9,6 1,10,6 9,1,7 8,2,7 And ..continue.. From this output i only need 1 to 9 range in one row 9 , 7 , 1 8 , 8 , 1 7 , 9 , 1 9 ,6 ,2 8 ,7 ,2 7, 8 ,2 6 ,9 ,2 3,1,3 9,5,3 8,6,3 7,7,3 6,8,3 5,9,3 9,4,4 8,5,4 7,6,4 6,7,4 5,8,4 4,9,4 9,3,5 8,4,5 7,5,5 6,6,5 5,7,5 4,8,5 3,9,5 9,2,6 8,3,6 7,4,6 6,5,6 5,6,6 4,7,6 3,8,6 2,9,6 9,1,7

Ответов - 0



полная версия страницы