fork(6) download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <iterator>
  4. #include <vector>
  5.  
  6. void print(std::vector<int>& a) {
  7. std::ostream_iterator<int> i(std::cout, " ");
  8. std::copy(a.begin(), a.end(), i);
  9. std::cout << "\n";
  10. }
  11.  
  12. void recurse(std::vector<int>& a, int pos, int remaining) {
  13. if (remaining == 0) { print(a); return; }
  14. if (pos == a.size()) { return; }
  15. for (int i = remaining; i >= 0; --i) {
  16. a[pos] = i;
  17. recurse(a, pos + 1, remaining - i);
  18. }
  19. }
  20.  
  21. int main() {
  22. int sum_val = 2, n = 5;
  23. std::vector<int> a(n);
  24. recurse(a, 0, sum_val);
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
2 0 0 0 0 
1 1 0 0 0 
1 0 1 0 0 
1 0 0 1 0 
1 0 0 0 1 
0 2 0 0 0 
0 1 1 0 0 
0 1 0 1 0 
0 1 0 0 1 
0 0 2 0 0 
0 0 1 1 0 
0 0 1 0 1 
0 0 0 2 0 
0 0 0 1 1 
0 0 0 0 2