fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int n, sum, stack[10];
  6.  
  7. void solve(int level) {
  8. if(sum == n) {
  9. for(int i = 1; i < level; ++i) {
  10. cout<<stack[i]<<" ";
  11. }
  12. cout<<endl;
  13. } else {
  14. if (level == 1) stack[level] = 0; else stack[level] = stack[level-1]-1;
  15. while(stack[level] + sum < n) {
  16. stack[level]+=1;
  17. sum += stack[level];
  18. solve(level+1);
  19. sum -= stack[level];
  20. }
  21. }
  22.  
  23. }
  24. int main(int argc, char const *argv[]) {
  25.  
  26. sum = 0;
  27. n = 5;
  28. solve(1);
  29. return 0;
  30. }
  31.  
Success #stdin #stdout 0.01s 5424KB
stdin
Standard input is empty
stdout
1 1 1 1 1 
1 1 1 2 
1 1 3 
1 2 2 
1 4 
2 3 
5