fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void combinationGen(int B[], int K, int N, bool &finished) {
  5. int i = K;
  6. while (i > 0 && B[i] == N - K + i) i--;
  7. if (i == 0) {
  8. finished = true;
  9. } else {
  10. B[i]++;
  11. for (int j = i + 1; j <= K; j++) B[j] = B[i] + j - i;
  12. }
  13. }
  14.  
  15. void generateCombinations(int n, int k) {
  16. int B[20];
  17. bool finished = false;
  18. for (int i = 1; i <= k; i++) B[i] = i;
  19.  
  20. while (!finished) {
  21. for (int i = 1; i <= k; i++) {
  22. cout << B[i] << " ";
  23. }
  24. cout << endl;
  25. combinationGen(B, k, n, finished);
  26. }
  27. }
  28.  
  29. int main() {
  30. int n, k;
  31. cin >> k >> n;
  32. generateCombinations(n, k);
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0.01s 5260KB
stdin
2 3
stdout
1 2 
1 3 
2 3