x2311

Untitled

Dec 22nd, 2021 (edited)
666
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include <iostream>
  2. #include <bitset>
  3. #include <cmath>
  4.  
  5. typedef unsigned char uc;
  6. const int size_uc = 8 * sizeof(uc);
  7. using namespace std;
  8.  
  9. uc Gray(const uc &n) {
  10.     return (n ^ (n >> 1));
  11. }
  12.  
  13.  
  14. int main() {
  15.     cout << "n:";
  16.     int n;
  17.     cin >> n;
  18.     for (int i = 0; i < pow(2,n); ++i) {
  19.         uc x = i;
  20.         uc gray_x = Gray(x);
  21.         bitset<8> s = bitset<size_uc>(gray_x);
  22.         string s1;
  23.         for (int j = 0; j < n; ++j) {
  24.             s1+= to_string(s[j]);
  25.  
  26.         }
  27.         string s2;
  28.         for (int j = 0; j < s1.length(); ++j) {
  29.             s2+= s1[s1.length()-j-1];
  30.         }
  31.         cout << s2 << "\n";
  32.        
  33.     }
  34.     return 0;
  35. }
Add Comment
Please, Sign In to add comment