#include <iostream> void general(int N, int k); int main() { using namespace std; general(1, 0); cout << endl; general(1, 1); cout << endl; general(1, 2); cout << endl; general(2, 0); cout << endl; general(2, 1); cout << endl; general(2, 2); cout << endl; general(3, 0); cout << endl; general(3, 1); cout << endl; general(3, 2); cout << endl; return 0; } void inner(int N, int k, int* temp, int round) { using namespace std; for (int i = 1; i <= N ;i++) { if (k > 0) { temp[round] = i; inner(i, k - 1, temp, round + 1); } else { cout << "["; for (int j = 0; j < round; j++) cout << temp[j] << ","; cout << i; cout << "] "; } } } void general(int N, int k) { int* temp = new int[k]; inner(N, k, temp, 0); delete [] temp; }
Standard input is empty
[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]