Advertisement
pexea12

Tổ hợp

Sep 25th, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.49 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void print(int a[], int n) {
  6.     cout << endl;
  7.     for (int i = 1; i <= n; ++i)
  8.         cout << a[i] << " ";
  9. }
  10. int main()
  11. {
  12.     int n, k;
  13.     int a[100];
  14.    
  15.     cout << "n = "; cin >> n;
  16.     cout << "k = "; cin >> k;
  17.    
  18.     for (int i = 1; i <= k; ++i) a[i] = i;
  19.    
  20.     int i;
  21.     do {
  22.         print(a, k);
  23.         i = k;
  24.         while (i > 0 && a[i] == n - k + i) --i;
  25.         if (i > 0) {
  26.             ++a[i];
  27.             for (int j = i + 1; j <= k; ++j) a[j] = a[j - 1] + 1;
  28.         }
  29.     } while (i > 0);
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement