done 3 sorting problems
This commit is contained in:
32
CSES - CSES Problem Set/Introducotry_Problems/Gray_Code.cpp
Normal file
32
CSES - CSES Problem Set/Introducotry_Problems/Gray_Code.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
// 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;
|
||||
}
|
||||
Reference in New Issue
Block a user