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