Advertisement
STANAANDREY

combi pe bits

Jun 18th, 2020
1,443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define MAXBITS 32
  4.  
  5. int main()
  6. {
  7.     int n, k;
  8.     cin >> n >> k;
  9.     assert(n >= k && n <= 10);
  10.     int b = 1 << n;
  11.     for (int a = 0; a < b; a++)
  12.     {
  13.         cerr << "nr: " << a << ' ';
  14.         cerr << "bits(rev): ";
  15.         for (int i = 0; i < MAXBITS; i++)
  16.             cerr << bool(a & (1 << i));//repr e inversa
  17.         cerr << "poz: ";
  18.         for (int i = 0; i < MAXBITS; i++)
  19.             if (a & (1 << i))
  20.                 cerr << i << ' ';
  21.         //comb
  22.         cerr << "comb: ";
  23.         int cnt = 0;
  24.         for (int i = 0; i < MAXBITS; i++)
  25.             if (a & (1 << i))
  26.                 cnt++;
  27.         if (cnt == k)
  28.             for (int i = 0; i < MAXBITS; i++)
  29.                 if (a & (1 << i))
  30.                     cerr << i << ' ';
  31.         cerr << endl;
  32.     }
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement