1721. Gray Code [Special Judge]

Source: Internet
Author: User
Tags creative commons attribution
1721. Gray Code [Special Judge]  
 
Total: 2124 Accepted: 638 Rating: 2.7/5.0 (12 votes)  
 
     
     
  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. 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.