fork(4) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void print(const int *arr, int n) {
  5. for(int i = 0; i < n; ++i)
  6. cout << arr[i] << " ";
  7. cout << endl;
  8. }
  9.  
  10. void printIncreasingNumbers(int n, int k) {
  11. if (k == 0) return;
  12. int *startNumArr = new int[k];
  13. for(int i = 0; i < k; ++i) startNumArr[i] = 1 + i;
  14.  
  15. int curPos = k - 1;
  16. while(curPos >= 0) {
  17. print(startNumArr, k);
  18. while(curPos >= 0) {
  19. ++startNumArr[curPos];
  20. if(n - startNumArr[curPos] >= k - curPos - 1) {
  21. for(int i = 1; i <= k - curPos - 1; ++i)
  22. startNumArr[curPos + i] = startNumArr[curPos] + i;
  23. curPos = k - 1; break;
  24. }
  25. else
  26. --curPos;
  27. }
  28. }
  29. delete [] startNumArr;
  30. }
  31.  
  32. int main() {
  33. int n = 7, k = 3;
  34. printIncreasingNumbers(n, k);
  35. return 0;
  36. }
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
1 2 3 
1 2 4 
1 2 5 
1 2 6 
1 2 7 
1 3 4 
1 3 5 
1 3 6 
1 3 7 
1 4 5 
1 4 6 
1 4 7 
1 5 6 
1 5 7 
1 6 7 
2 3 4 
2 3 5 
2 3 6 
2 3 7 
2 4 5 
2 4 6 
2 4 7 
2 5 6 
2 5 7 
2 6 7 
3 4 5 
3 4 6 
3 4 7 
3 5 6 
3 5 7 
3 6 7 
4 5 6 
4 5 7 
4 6 7 
5 6 7