C ++ classical algorithm problem - Gray code (Gray Code)

28.Algorithm Gossip: Gray code (Gray Code)

Explanation

Gray Code is a set number of columns, each of the number of bits used to represent the binary, it is assumed that the number n of bits to represent each well, and only a different bit values ​​between any two numbers, for example, 3-bit Gray Code:

000 001 011 010 110 111 101 100

By definition may know, the order of Gray Code is not the only example above, the number of columns in turn to write, but also a set of Gray Code:

100 101 111 110 010 011 001 000

Gray Code by Frank Gray raised by Bell Labs in the 1940s, to avoid making mistakes when using PCM (Pusle Code Modulation) signal transmission method, and on March 17, 1953 obtained US patents.

solution

Since adjacent Gray Code only a change between two bit number, it is possible to observe the Gray Code from 1 to 0 or from 0 to 1 at the time of change, given 4-bit Gray Code as follows:

0000 0001 0011 0010 0110 0111 0101 0100
1100 1101 1111 1110 1010 1011 1001 1000

When the observed changes in the odd term, we find whether it is the first of several Gray Code, forever changing only the rightmost bit, if it is 1 to 0, if it is 0 to 1.

When the observed changes in even-numbered, we found that the changed bits, is considered the right to the left side of bit 1 of the first.

Two or more variation rule is fixed, regardless of the number of bits that; so long as the position determining bit is odd or even, which can decide to change the value of a bit, in order to write programming convenience, the array index 0 as rightmost value, and in the print result, large numbers began to reverse by the index print.

Of 2-bit Gray Code as the coordinate plane view, may constitute a quadrilateral, you can find any vertex from around the perimeter of a quadrilateral perimeter, through which a set of vertex coordinates is the Gray Code, so You can get four Gray Code.

The same of 3 bits Gray Code as plane coordinates of view, can constitute a positive cube, if you can start from any vertex through all the side length, after the vertex is not repeated if, after the the combination of the vertex coordinates is a set of sequential Gray Code.

The sample code

#include <stdio.h> 
#include <stdlib.h>
#define MAXBIT 20
#define TRUE 1
#define CHANGE_BIT(x) x = ((x) == '0' ? '1' : '0')
#define NEXT(x) x = (1 - (x))

    int main(void) {
        char digit[MAXBIT]; int i, bits, odd;

        printf("输入位元数:"); scanf("%d", &bits);

        for(i = 0; i < bits; i++) { digit[i] = '0';
            printf("0");
        }
        printf("\n"); odd = TRUE; while(1) {
            if(odd)
                CHANGE_BIT(digit[0]);
            else {
                // 计算第一个1的位置
                for(i = 0; i < bits && digit[i] == '0'; i++) ; if(i == bits - 1) // 最后一个Gray Code
                    break; CHANGE_BIT(digit[i+1]);
            }
            for(i = bits - 1; i >= 0; i--)
                printf("%c", digit[i]);

            printf("\n"); NEXT(odd);
        }

        return 0;
    }

发布了1133 篇原创文章 · 获赞 928 · 访问量 6万+

Guess you like

Origin blog.csdn.net/weixin_42528266/article/details/104019495