|
Time Limit: 1sec memory limit: 32 mbdescription Gray code is an interesting code sequence and has processing applications in computer science. no matter you have known it before or not, here are some introductions about its features: (1) Gray Code has 2 N unique elements; (2) Each element contains N digits of 0 or 1; (3) Each pair of adjacent elements has exactly one different digit. For example, when n = 2, one of the gray code sequences is. Now, the task is quite simple, given a positive integer N, generate the corresponding Gray code sequence. Input input may contain in multiple test cases. each test case consists of one positive integer n (n <= 16), input is terminated by a case with n = 0, which shocould not be processed. output for each test case, output the corresponding Gray code sequence, one element per line. there may be multiple answers, any of them will be accepted. please output a blank line after each test case. sample input copy sample input to clipboard 120 Sample output 0100011110 // Problem #: 1721 // submission #: 2001703 // the source code is licensed under Creative Commons Attribution-non1_cial-since Alike 3.0 unported license // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/// all copyright reserved by informatic lab of Sun Yat-sen University # include <iostream> # include <cmath> # include <stdio. h ># include <string> using namespace STD; void change (char * P, int N) {P [n-1] + = 1; Int I = 0; while (P [n-1-i]> = '2') {P [n-1-i]-= 2; p [n-2-i] + = 1; I ++ ;}} void Haha (char * P, char * q, int N) {int I; Q [0] = P [0]; for (I = 1; I <N; I ++) {q [I] = (P [I-1]-'0') ^ (P [I]-'0') + '0 ';}} int main () {int N; while (CIN> N & n! = 0) {char * P = new char [n + 1]; char * q = new char [n + 1]; int I; P [N] = '\ 0'; Q [N] =' \ 0'; for (I = 0; I <n; I ++) {P [I] = '0'; Q [I] = '0';} for (I = 0; I <POW (2, n); I ++) {int X; printf ("% s \ n", q); change (P, n); Haha (p, q, n );} printf ("\ n");} return 0 ;} output with printf, and must be converted to the string required by the question, rather than simply adding one.
|