33 lines
719 B
C++
33 lines
719 B
C++
// Gray Code
|
|
|
|
#include<bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
|
|
vector<string> gray_code(int size, bool print) {
|
|
if (size == 1) {
|
|
if (print) {
|
|
cout << "0" << '\n' << "1" << endl;
|
|
}
|
|
return {"0", "1"};
|
|
}
|
|
auto prev_gray_code = gray_code(size - 1, false);
|
|
vector<string> output;
|
|
for (auto &s: prev_gray_code) {
|
|
if (print) cout << "0"+s << endl;
|
|
output.push_back("0"+s);
|
|
}
|
|
std::reverse(prev_gray_code.begin(), prev_gray_code.end());
|
|
for (auto &s: prev_gray_code) {
|
|
if (print) cout << "1"+s << endl;
|
|
output.push_back("1"+s);
|
|
}
|
|
return output;
|
|
}
|
|
|
|
int main() {
|
|
int n; cin >> n;
|
|
gray_code(n, true);
|
|
return 0;
|
|
}
|