Advertisement
Okorosso

Untitled

Mar 25th, 2021
545
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.79 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. bool nextCombination(std::vector<int> &a, int n, int m) {
  5.     int k = m;
  6.     for (int i = k - 1; i >= 0; --i)
  7.         if (a[i] < n - k + i + 1) {
  8.             ++a[i];
  9.             for (int j = i + 1; j < k; ++j)
  10.                 a[j] = a[j - 1] + 1;
  11.             return true;
  12.         }
  13.     return false;
  14. }
  15.  
  16. void printCombs(std::vector<int> &a, int n) {
  17.     using namespace std;
  18.     for (int i = 0; i < n; i++)
  19.         cout << a[i] << " ";
  20.     cout << '\n';
  21. }
  22.  
  23. int main() {
  24.     using namespace std;
  25.     int n, k;
  26.     cin >> n >> k;
  27.     vector<int> a(n);
  28.     for (int i = 0; i < n; i++)
  29.         a[i] = i + 1;
  30.  
  31.     printCombs(a, k);
  32.     if (n >= k) {
  33.         while (nextCombination(a, n, k))
  34.             printCombs(a, k);
  35.     }
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement