aboutsummaryrefslogtreecommitdiff
path: root/01_introductory_problems/gray_code_2205.cpp
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2026-07-06 19:08:08 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2026-07-06 19:08:08 +0200
commit96254947699986c59f0dc63d69fd4b76bd3ed43e (patch)
tree6c4dca945d7f7427c48be234d827fe4d33be02c5 /01_introductory_problems/gray_code_2205.cpp
downloadcses-96254947699986c59f0dc63d69fd4b76bd3ed43e.tar.gz
cses-96254947699986c59f0dc63d69fd4b76bd3ed43e.zip
Initial commit
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