Gray Code C + + implementation

Source: Internet
Author: User

Gray Code C + + implementation

Topic

Given an integer n, return the N-bit gray code, which starts with 0 and requires a recursive implementation.

Gray code:

In the code of a group of numbers, if any two adjacent code is different from only one binary number, it is called the Gray Code, in addition, because the maximum number and the minimum number is only one digit difference, namely "end-to-end", so also called cyclic code or reflection code.

Http://baike.baidu.com/link?url=GNm2SqnlhGiX-Y3_TStF_U9cQJYERb39y-c-3INJhpiiU6B8YZ5Pu9bZCHZiGPid8g8OB3Fvf9Z_3vTMZypfFK

See various codes for more details

2 Problem Analysis

Actually initially see the simple gray code definition, as well as the title requirements are completely blindfolded. I don't know what the hell this is.

Until..... Just Baidu, seriously read, just understand what a ghost ~|~

The plain thing is to print an n-bit binary code, ranging from 0 to 2 N-1.

As for recursive realization, a trifle (the key or to understand test instructions/kb)

And then there's nothing to say, direct realization.

voidGraycode (int&x,Const intMax,Const intN) {    intTMP =x; if(X >Max) {        return; }     for(inti = N-1; I >=0; --i) {cout<< ((x >> i) &1); } cout<<Endl; Graycode (++x, Max,n);}voidTestgray () {intx =0; intMax = to; intn =5; Graycode (x, Max, n);}

Note:

I just realized the function simply.

As for the input and output requirements of others, smiled and passed.

You can encapsulate the function, the outer interface conforms to the problem. All of the other functions are your implementation.

Gray Code C + + implementation

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.