From 5b765ce70d76f1c3e56b24694c832bd938d33816 Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Fri, 14 Feb 2025 12:35:30 -0700 Subject: 2205. gray code --- introductory_problems/gray_code.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 introductory_problems/gray_code.cpp (limited to 'introductory_problems/gray_code.cpp') 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 +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(); + } +} -- cgit v1.2.3