-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2205.cpp
56 lines (50 loc) · 1.23 KB
/
2205.cpp
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl "\n"
void generate_gray_codes(int n)
{
int arr[1 << n + 1];
memset(arr, 0, sizeof(arr));
int i;
for(i = 0; i < n; i++)
{
cout << 0;
}
cout << endl;
arr[0] = 1;
int x = 0;
for(i = 0;i < 1 << n; i++)
{
for(int j = 0; j < n; j++)
{
int y = x ^ (1 << j);
if (!arr[y])
{
x = y;
arr[y] = 1;
string s;
while (y)
{
if (y&1) s += '1';
else s += '0';
y >>= 1;
}
reverse(s.begin(), s.end());
for (int i = 0; i < n - s.size(); i++)
cout<<0;
cout<<s<<endl;
break;
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
generate_gray_codes(n);
return 0;
}