summaryrefslogtreecommitdiff
path: root/introductory_problems/gray_code.cpp
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-02-14 12:35:30 -0700
committerJack Sangdahl <[email protected]>2025-02-14 12:35:30 -0700
commit5b765ce70d76f1c3e56b24694c832bd938d33816 (patch)
tree9b1eb1b9843c0d058b2603348ba7c314b2cdb97a /introductory_problems/gray_code.cpp
parent06922738b735ac453245e72621c54a8ccea4f3c6 (diff)
downloadcses-5b765ce70d76f1c3e56b24694c832bd938d33816.tar.gz
cses-5b765ce70d76f1c3e56b24694c832bd938d33816.zip
2205. gray code
Diffstat (limited to 'introductory_problems/gray_code.cpp')
-rw-r--r--introductory_problems/gray_code.cpp23
1 files changed, 23 insertions, 0 deletions
diff --git a/introductory_problems/gray_code.cpp b/introductory_problems/gray_code.cpp
new file mode 100644
index 0000000..9911cd4
--- /dev/null
+++ b/introductory_problems/gray_code.cpp
@@ -0,0 +1,23 @@
+// 2205. Gray Code
+#include <bits/stdc++.h>
+using namespace std;
+
+static int n;
+const int max_n = 16;
+static bool a[max_n + 1];
+
+void print(void) {
+ for (int i = n; i; i--)
+ printf("%d", a[i]);
+ printf("\n");
+}
+
+int main() {
+ scanf("%d", &n);
+ print();
+ for (int i = 1; i < 1 << n; i++) {
+ int lsb = __builtin_ffs(i);
+ a[lsb] ^= 1;
+ print();
+ }
+}