aboutsummaryrefslogtreecommitdiff
path: root/01_introductory_problems/gray_code_2205.cpp
diff options
context:
space:
mode:
Diffstat (limited to '01_introductory_problems/gray_code_2205.cpp')
-rw-r--r--01_introductory_problems/gray_code_2205.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/01_introductory_problems/gray_code_2205.cpp b/01_introductory_problems/gray_code_2205.cpp
new file mode 100644
index 0000000..4fa8a2d
--- /dev/null
+++ b/01_introductory_problems/gray_code_2205.cpp
@@ -0,0 +1,22 @@
1#include <iostream>
2#include <format>
3
4void print(int number, int ndigits) {
5 std::cout << std::format("{0:0{1}b}", number, ndigits) << "\n";
6}
7
8void print_all(int& start, int digit, int ndigits) {
9 if (digit == 0) {
10 print(start, ndigits);
11 } else {
12 print_all(start, digit-1, ndigits);
13 start ^= 1 << (digit-1);
14 print_all(start, digit-1, ndigits);
15 }
16}
17
18int main() {
19 int n, start{0};
20 std::cin >> n;
21 print_all(start, n, n);
22}

Generated with cgit - Back to sebastiano.tronto.net