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