Advertisement
Josif_tepe

Untitled

Feb 16th, 2022
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.67 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int n;
  5.  
  6. void print_binary(int x) {
  7.     string binary = "";
  8.     while(x > 0) {
  9.         binary += (x % 2) + '0';
  10.         x /= 2;
  11.     }
  12.     while(binary.size() < n) {
  13.         binary += '0';
  14.     }
  15.     reverse(binary.begin(), binary.end());
  16.     cout << "{";
  17.     for(int i =0 ; i < binary.size(); i++) {
  18.         if(binary[i] == '1') {
  19.             cout << i + 1 << ",";
  20.         }
  21.     }
  22.     cout << "}" << endl;
  23. }
  24. int main()
  25. {
  26.     cin >> n;
  27.     int power = 1;
  28.     for(int i = 1; i <= n; i++) {
  29.         power *= 2;
  30.     }
  31.     for(int i = 0; i < power; i++) {
  32.         print_binary(i);
  33.     }
  34.     return 0;
  35. }
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement