fork(1) download
  1. #include <iostream>
  2.  
  3. void general(int N, int k);
  4.  
  5. int main()
  6. {
  7. using namespace std;
  8.  
  9. general(1, 0);
  10. cout << endl;
  11.  
  12. general(1, 1);
  13. cout << endl;
  14.  
  15. general(1, 2);
  16. cout << endl;
  17.  
  18. general(2, 0);
  19. cout << endl;
  20.  
  21. general(2, 1);
  22. cout << endl;
  23.  
  24. general(2, 2);
  25. cout << endl;
  26.  
  27. general(3, 0);
  28. cout << endl;
  29.  
  30. general(3, 1);
  31. cout << endl;
  32.  
  33. general(3, 2);
  34. cout << endl;
  35.  
  36. return 0;
  37. }
  38.  
  39. void inner(int N, int k, int* temp, int round)
  40. {
  41. using namespace std;
  42.  
  43. for (int i = 1; i <= N ;i++)
  44. {
  45. if (k > 0)
  46. {
  47. temp[round] = i;
  48. inner(i, k - 1, temp, round + 1);
  49. }
  50. else
  51. {
  52. cout << "[";
  53. for (int j = 0; j < round; j++)
  54. cout << temp[j] << ",";
  55. cout << i;
  56. cout << "] ";
  57. }
  58. }
  59. }
  60.  
  61. void general(int N, int k)
  62. {
  63. int* temp = new int[k];
  64.  
  65. inner(N, k, temp, 0);
  66.  
  67. delete [] temp;
  68. }
Success #stdin #stdout 0s 2984KB
stdin
Standard input is empty
stdout
[1] 
[1,1] 
[1,1,1] 
[1] [2] 
[1,1] [2,1] [2,2] 
[1,1,1] [2,1,1] [2,2,1] [2,2,2] 
[1] [2] [3] 
[1,1] [2,1] [2,2] [3,1] [3,2] [3,3] 
[1,1,1] [2,1,1] [2,2,1] [2,2,2] [3,1,1] [3,2,1] [3,2,2] [3,3,1] [3,3,2] [3,3,3]