Advertisement
STANAANDREY

combNbyK (IA)

Jan 11th, 2020
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define newline '\n'
  3. #define ll long long
  4. #define uns unsigned
  5. #define deb(x) clog << x << ' '
  6. #define debnewL(x) clog << x << newline
  7. using namespace std;
  8. ///************************************
  9. inline void __attribute__ ((constructor)) _cfun();
  10. inline void _cfun()
  11. {
  12.     freopen("combinari.in", "r", stdin);
  13.     freopen("combinari.out", "w", stdout);
  14.     ios :: sync_with_stdio(NULL);
  15.     cin.tie(nullptr);
  16.     cout.tie(nullptr);
  17. }
  18. ///************************************
  19.  
  20. int n, k;
  21. int st[20];
  22.  
  23. inline void display()
  24. {
  25.     for (int i = 1; i <= k; i++)
  26.         cout << st[i] << ' ';
  27.     cout << newline;
  28. }
  29.  
  30. void backtracking(int top)
  31. {
  32.     if (top == k + 1)
  33.     {
  34.         display();
  35.         return;
  36.     }
  37.  
  38.     for (int i = st[top - 1] + 1; i <= n; i++)
  39.     {
  40.         st[top] = i;
  41.         backtracking(top + 1);
  42.     }
  43. }
  44.  
  45. signed main()
  46. {
  47.     cin >> n >> k;
  48.     backtracking(1);
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement