Java recursive implementation of gray Code (grey code) __java

Source: Internet
Author: User
Tags pow
Problem: All gray codes that produce n bits.
Gray code is a collection of numbers, each of which is represented by a binary, assuming that an n-bit is used to represent each number, and that there is only one bit difference between any two numbers. For example, the following is a 3-bit gray code: 000 001 011 010 110 111 101 100. If you want to generate an n-bit gray code, then the number of gray code is 2^n.
Assuming that the original value starts at 0, the rule of gray code is: the first step is to change the rightmost bit value; The second step is to change the left bit of the bit with the first 1 on the right and the third step, the fourth step is to repeat the first and second steps until all the gray codes are produced (in other words, have gone (2^n)-1 steps).
Use an example to illustrate: suppose to generate 3-bit gray code, the original value bit 000 first step: Change the rightmost bit value: 001 The second step: change the left bit of the bit with the first 1 on the right: 011 Third: Change the rightmost bit value: 010 Step Fourth: Change the left bit of the bit with the right first 1: 110 Fifth Step: Change the rightmost bit value: 111 Sixth step: Change the left bit of the first 1-bit on the right: 101 Seventh Step: Change the rightmost bit value: 100
If you follow this rule to generate the gray code, there is no problem, but this is too complicated. If you look carefully at the structure of the Gray code, we will have the following findings: 1, in addition to the highest bit (the left first), gray code of the bits are fully up and down symmetry (see the list below). For example, the first gray code is symmetric with the last gray code (except the first one), the second Gray code and the penultimate symmetry, and so on. 2, the smallest repeating unit is 0, 1.
000
001
011
010
110
111
101
100

So, at the time of implementation, we can use recursion, plus 0 or 1 in front of each layer, and then we can list all the gray codes. For example: First step: Generate 0, 12 strings. Step two: On the basis of the first step, each string is added 0 and 1, but only one can be added at a time, so it must be done two times. This becomes the 00,01,11,10 (pay attention to symmetry). The third step: on the basis of the second step, add 0 and 1 to each string, again, add one at a time, and then it becomes 000,001,011,010,110,111,101,100. All right, so we're going to generate a 3-bit gray code. If you want to generate 4-bit code, we just need to add another layer of 0,1 to the 3-bit code: 0000,0001,0011,0010,0110,0111,0101,0100,1100,1101,1110,1010,0111,1001,1000.
Other words N-bit gray code is based on the n-1 bit of gray code generated.

If you can understand the above section, the following section of the code implementation is easy to understand.

Package Graycode;

Import Java.util.Scanner;

public class Solution {public
	
    static string[] Graycode (int n) {
    	
    	//produce 2^n grade Codes string[
    	] Graycode = New string[(int) Math.pow (2, N)];
    	if (n = = 1) {
    		graycode[0] = "0";
    		GRAYCODE[1] = "1";
    		return graycode;
    	}
    	
    	string[] last = Graycode (n-1);
    	
    	for (int i = 0; i < last.length i++) {
    		Graycode[i] = "0" + last[i];
    		Graycode[graycode.length-1-I] = "1" + last[i];
    	}
    	
    	return graycode;
    }
    
    public static void Main (string[] args) {
    	Scanner in = new Scanner (system.in);  	
    	int n = in.nextint (); 
    	In.close ();
    	String[] codes = new string[(int) Math.pow (2, N)];
    	codes = Graycode (n);
    	for (string string:codes) {
			System.out.println (string);
		}
    }
}


Related Article

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.