📜  生成n位格雷码|套装2

📅  最后修改于: 2021-04-28 17:31:40             🧑  作者: Mango

给定数字n,请生成从0到2 ^ n-1的位模式,以使连续的模式相差一位。

例子:

已经讨论了生成n位格雷码的另一种方法。

方法:
这个想法是使用XOR和Right shift操作获得二进制数的格雷码。

  1. 格雷码的第一位(MSB)与二进制数的第一位(MSB)相同。
  2. 格雷码的第二位(从左侧开始)等于二进制数的第一位(MSB)与第二位(2nd MSB)的XOR。
  3. 格雷码的第三位(从左侧开始)等于第二位(第二MSB)和第三位(第三MSB)的XOR,依此类推。

这样,可以为相应的二进制数计算格雷码。因此,可以观察到,第i个元素可以由i和floor(i / 2)的按位XOR形成,等于i和(i >> 1)的按位XOR,即i右移1。通过执行此操作,二进制数的MSB保持不变,并且所有其他位与其相邻的更高位按位进行XOR。

C++
// C++ program to generate n-bit
// gray codes
#include 
using namespace std;
 
// Function to convert decimal to binary
void decimalToBinaryNumber(int x, int n)
{
    int* binaryNumber = new int(x);
    int i = 0;
    while (x > 0) {
        binaryNumber[i] = x % 2;
        x = x / 2;
        i++;
    }
 
    // leftmost digits are filled with 0
    for (int j = 0; j < n - i; j++)
        cout << '0';
 
    for (int j = i - 1; j >= 0; j--)
        cout << binaryNumber[j];
}
 
// Function to generate gray code
void generateGrayarr(int n)
{
    int N = 1 << n;
    for (int i = 0; i < N; i++) {
 
        // generate gray code of corresponding
        // binary number of integer i.
        int x = i ^ (i >> 1);
 
        // printing gray code
        decimalToBinaryNumber(x, n);
 
        cout << endl;
    }
}
 
// Drivers code
int main()
{
    int n = 3;
    generateGrayarr(n);
    return 0;
}


Java
// Java program to generate
// n-bit gray codes
import java.io.*;
 
class GFG {
 
    // Function to convert
    // decimal to binary
    static void decimalToBinaryNumber(int x,
                                      int n)
    {
        int[] binaryNumber = new int[x];
        int i = 0;
        while (x > 0) {
            binaryNumber[i] = x % 2;
            x = x / 2;
            i++;
        }
 
        // leftmost digits are
        // filled with 0
        for (int j = 0; j < n - i; j++)
            System.out.print('0');
 
        for (int j = i - 1;
             j >= 0; j--)
            System.out.print(binaryNumber[j]);
    }
 
    // Function to generate
    // gray code
    static void generateGrayarr(int n)
    {
        int N = 1 << n;
        for (int i = 0; i < N; i++) {
 
            // generate gray code of
            // corresponding binary
            // number of integer i.
            int x = i ^ (i >> 1);
 
            // printing gray code
            decimalToBinaryNumber(x, n);
 
            System.out.println();
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 3;
        generateGrayarr(n);
    }
}
 
// This code is contributed
// by anuj_67.


Python3
# Python program to generate
# n-bit gray codes
 
# Function to convert
# decimal to binary
def decimalToBinaryNumber(x, n):
    binaryNumber = [0]*x;
    i = 0;
    while (x > 0):
        binaryNumber[i] = x % 2;
        x = x // 2;
        i += 1;
 
    # leftmost digits are
    # filled with 0
    for j in range(0, n - i):
        print('0', end ="");
 
    for j in range(i - 1, -1, -1):
        print(binaryNumber[j], end ="");
 
# Function to generate
# gray code
def generateGrayarr(n):
    N = 1 << n;
    for i in range(N):
         
        # generate gray code of
        # corresponding binary
        # number of integer i.
        x = i ^ (i >> 1);
 
        # printing gray code
        decimalToBinaryNumber(x, n);
 
        print();
 
# Driver code
if __name__ == '__main__':
    n = 3;
    generateGrayarr(n);
 
# This code is contributed by 29AjayKumar


C#
// C# program to generate
// n-bit gray codes
using System;
 
class GFG {
 
    // Function to convert
    // decimal to binary
    static void decimalToBinaryNumber(int x,
                                      int n)
    {
        int[] binaryNumber = new int[x];
        int i = 0;
        while (x > 0) {
            binaryNumber[i] = x % 2;
            x = x / 2;
            i++;
        }
 
        // leftmost digits are
        // filled with 0
        for (int j = 0; j < n - i; j++)
            Console.Write('0');
 
        for (int j = i - 1;
             j >= 0; j--)
            Console.Write(binaryNumber[j]);
    }
 
    // Function to generate
    // gray code
    static void generateGrayarr(int n)
    {
        int N = 1 << n;
        for (int i = 0; i < N; i++) {
 
            // Generate gray code of
            // corresponding binary
            // number of integer i.
            int x = i ^ (i >> 1);
 
            // printing gray code
            decimalToBinaryNumber(x, n);
 
            Console.WriteLine();
        }
    }
 
    // Driver code
    public static void Main()
    {
        int n = 3;
        generateGrayarr(n);
    }
}
 
// This code is contributed
// by anuj_67.


输出
000
001
011
010
110
111
101
100

复杂度分析:

  • 时间复杂度: O(2 n )。
    从0到(2 n )只需一个遍历。
  • 辅助空间: O(log x)。
    (x)的二进制表示需要空格(log x)