diff options
Diffstat (limited to 'introductory_problems/gray_code.cpp')
-rw-r--r-- | introductory_problems/gray_code.cpp | 23 |
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(); + } +} |