forked from MAYANK25402/Hactober-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrayCode.cpp
55 lines (49 loc) · 967 Bytes
/
GrayCode.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
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define forn(i, n) for (int i = 0; i < int(n); i++)
// freopen('input.txt', 'r', stdin);
// freopen('output.txt', 'w', stdout);
vector<string> gray(int n)
{
vector<string> v;
if (n == 1)
{
v.PB("0");
v.PB("1");
return v;
}
vector<string> v1 = gray(n - 1);
int size = v1.size();
for (int i = 0; i < size; i++)
{
v.PB("0" + v1[i]);
}
for (int i = size - 1; i >= 0; i--)
{
v.PB("1" + v1[i]);
}
return v;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// int _t;cin>>_t; while(_t--)
int n;
cin >> n;
vector<string> ans = gray(n);
for (auto x : ans)
{
cout << x << endl;
}
return 0;
}