-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGray_Code.cpp
More file actions
41 lines (29 loc) · 739 Bytes
/
Gray_Code.cpp
File metadata and controls
41 lines (29 loc) · 739 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Mohit Verma "mohitvdx"
// problem:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int INF = LLONG_MAX >> 1;
void solve(){
int n; cin>>n;
int total = 1<<n;
vector<string> ans;
for(int i=0; i<total; i++){
int gray = i^ (i>>1); //formula for calculating the hamming code
string binary = bitset<64>(gray).to_string(); //bitset converts the number into a set of bits if that number in binary
ans.push_back(binary.substr(64-n));
}
for(auto k:ans){
cout<<k<<'\n';
}
}
signed main(){
ios::sync_with_stdio(false); cin.tie(NULL); // fast IO
// int t; cin>>t;
// while(t--){
solve();
// }
}
/*
*/