📜  生成n位格雷码

📅  最后修改于: 2021-05-25 01:54:09             🧑  作者: Mango

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

例子:

Input: N = 2
Output: 00 01 11 10

Input: N = 3
Output: 000 001 011 010 110 111 101 100

方法1

上面的序列是不同宽度的格雷码。以下是格雷码中一个有趣的模式。
可以使用以下步骤从(n-1)位格雷码列表中生成n位格雷码。

  1. 令(n-1)位格雷码的列表为L1。创建另一个列表L2,它与L1相反。
  2. 通过在L1的所有代码中添加前缀“ 0”来修改列表L1。
  3. 通过在L2的所有代码中添加前缀“ 1”来修改列表L2。
  4. 连接L1和L2。级联列表是n位格雷码的必需列表

例如,以下是从2位格雷码列表的列表中生成3位格雷码列表的步骤。
L1 = {00,01,11,10}(2位格雷码列表)
L2 = {10,11,01,00}(L1的反向)
将L1的所有条目前缀为’0’,L1变为{000,001,011,010}
将L2的所有条目前缀为’1’,L2变为{110,111,101,100}
连接L1和L2,我们得到{000,001,011,010,110,111,101,100}
为了生成n位格雷码,我们从1位格雷码列表开始。 1位格雷码的列表为{0,1}。我们重复上述步骤,从1位格雷码生成2位格雷码,然后从2位格雷码生成3位格雷码,直到位数等于n。

下面是上述方法的实现:

C++
// C++ program to generate n-bit Gray codes
#include 
#include 
#include 
using namespace std;
 
// This function generates all n bit Gray codes and prints the
// generated codes
void generateGrayarr(int n)
{
    // base case
    if (n <= 0)
        return;
 
    // 'arr' will store all generated codes
    vector arr;
 
    // start with one-bit pattern
    arr.push_back("0");
    arr.push_back("1");
 
    // Every iteration of this loop generates 2*i codes from previously
    // generated i codes.
    int i, j;
    for (i = 2; i < (1<= 0 ; j--)
            arr.push_back(arr[j]);
 
        // append 0 to the first half
        for (j = 0 ; j < i ; j++)
            arr[j] = "0" + arr[j];
 
        // append 1 to the second half
        for (j = i ; j < 2*i ; j++)
            arr[j] = "1" + arr[j];
    }
 
    // print contents of arr[]
    for (i = 0 ; i < arr.size() ; i++ )
        cout << arr[i] << endl;
}
 
// Driver program to test above function
int main()
{
    generateGrayarr(3);
    return 0;
}


Java
// Java program to generate n-bit Gray codes
import java.util.*;
class GfG {
 
// This function generates all n bit Gray codes and prints the
// generated codes
static void generateGrayarr(int n)
{
    // base case
    if (n <= 0)
        return;
 
    // 'arr' will store all generated codes
    ArrayList arr = new ArrayList ();
 
    // start with one-bit pattern
    arr.add("0");
    arr.add("1");
 
    // Every iteration of this loop generates 2*i codes from previously
    // generated i codes.
    int i, j;
    for (i = 2; i < (1<= 0 ; j--)
            arr.add(arr.get(j));
 
        // append 0 to the first half
        for (j = 0 ; j < i ; j++)
            arr.set(j, "0" + arr.get(j));
 
        // append 1 to the second half
        for (j = i ; j < 2*i ; j++)
            arr.set(j, "1" + arr.get(j));
    }
 
    // print contents of arr[]
    for (i = 0 ; i < arr.size() ; i++ )
        System.out.println(arr.get(i));
}
 
// Driver program to test above function
public static void main(String[] args)
{
    generateGrayarr(3);
}
}


Python3
# Python3 program to generate n-bit Gray codes
import math as mt
 
# This function generates all n bit Gray
# codes and prints the generated codes
def generateGrayarr(n):
 
    # base case
    if (n <= 0):
        return
 
    # 'arr' will store all generated codes
    arr = list()
 
    # start with one-bit pattern
    arr.append("0")
    arr.append("1")
 
    # Every iteration of this loop generates
    # 2*i codes from previously generated i codes.
    i = 2
    j = 0
    while(True):
 
        if i >= 1 << n:
            break
     
        # Enter the prviously generated codes
        # again in arr[] in reverse order.
        # Nor arr[] has double number of codes.
        for j in range(i - 1, -1, -1):
            arr.append(arr[j])
 
        # append 0 to the first half
        for j in range(i):
            arr[j] = "0" + arr[j]
 
        # append 1 to the second half
        for j in range(i, 2 * i):
            arr[j] = "1" + arr[j]
        i = i << 1
 
    # prcontents of arr[]
    for i in range(len(arr)):
        print(arr[i])
 
# Driver Code
generateGrayarr(3)
 
# This code is contributed
# by Mohit kumar 29


C#
using System;
using System.Collections.Generic;
 
// C# program to generate n-bit Gray codes 
public class GfG
{
 
// This function generates all n bit Gray codes and prints the 
// generated codes 
public static void generateGrayarr(int n)
{
    // base case 
    if (n <= 0)
    {
        return;
    }
 
    // 'arr' will store all generated codes 
    List arr = new List ();
 
    // start with one-bit pattern 
    arr.Add("0");
    arr.Add("1");
 
    // Every iteration of this loop generates 2*i codes from previously 
    // generated i codes. 
    int i, j;
    for (i = 2; i < (1 << n); i = i << 1)
    {
        // Enter the prviously generated codes again in arr[] in reverse 
        // order. Nor arr[] has double number of codes. 
        for (j = i - 1 ; j >= 0 ; j--)
        {
            arr.Add(arr[j]);
        }
 
        // append 0 to the first half 
        for (j = 0 ; j < i ; j++)
        {
            arr[j] = "0" + arr[j];
        }
 
        // append 1 to the second half 
        for (j = i ; j < 2 * i ; j++)
        {
            arr[j] = "1" + arr[j];
        }
    }
 
    // print contents of arr[] 
    for (i = 0 ; i < arr.Count ; i++)
    {
        Console.WriteLine(arr[i]);
    }
}
 
// Driver program to test above function 
public static void Main(string[] args)
{
    generateGrayarr(3);
}
}
 
  // This code is contributed by Shrikant13


Javascript


C++
// C++ program to generate
// n-bit Gray codes
 
#include 
using namespace std;
 
// This function generates all n
// bit Gray codes and prints the
// generated codes
vector generateGray(int n)
{
    // Base case
    if (n <= 0)
        return {"0"};
 
    if (n == 1)
    {
      return {"0","1"};
    }
 
    //Recursive case
    vector recAns=
          generateGray(n-1);
    vector mainAns;
     
    // Append 0 to the first half
    for(int i=0;i=0;i--)
    {
       string s=recAns[i];
       mainAns.push_back("1"+s);
    }
    return mainAns;
}
 
// Function to generate the
// Gray code of N bits
void generateGrayarr(int n)
{
    vector arr;
    arr=generateGray(n);
    // print contents of arr
    for (int i = 0 ; i < arr.size();
         i++ )
        cout << arr[i] << endl;
}
 
// Driver Code
int main()
{
    generateGrayarr(3);
    return 0;
}


Java
// Java program to generate
// n-bit Gray codes
import java.io.*;
import java.util.*;
class GFG
{
 
  // This function generates all n
  // bit Gray codes and prints the
  // generated codes
  static ArrayList generateGray(int n)
  {
 
    // Base case
    if (n <= 0)
    {
      ArrayList temp =
        new ArrayList(){{add("0");}};
      return temp;
    }
    if(n == 1)
    {
      ArrayList temp =
        new ArrayList(){{add("0");add("1");}};
      return temp;
    }
 
    // Recursive case
    ArrayList recAns = generateGray(n - 1);
    ArrayList mainAns = new ArrayList();
 
    // Append 0 to the first half
    for(int i = 0; i < recAns.size(); i++)
    {
      String s = recAns.get(i);
      mainAns.add("0" + s);
 
    }
 
    // Append 1 to the second half
    for(int i = recAns.size() - 1; i >= 0; i--)
    {
      String s = recAns.get(i);
      mainAns.add("1" + s);
    }
    return mainAns;
  }
 
  // Function to generate the
  // Gray code of N bits
  static void generateGrayarr(int n)
  {
    ArrayList arr = new ArrayList();
    arr = generateGray(n);
 
    // print contents of arr
    for (int i = 0 ; i < arr.size(); i++)
    {
      System.out.println(arr.get(i));   
    }
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    generateGrayarr(3);
  }
}
 
// This code is contributed by rag2127.


Python3
# Python3 program to generate
# n-bit Gray codes
 
# This function generates all n
# bit Gray codes and prints the
# generated codes
def generateGray(n):
     
    # Base case
    if (n <= 0):
        return ["0"]
    if (n == 1):
        return [ "0", "1" ]
 
    # Recursive case
    recAns = generateGray(n - 1)
 
    mainAns = []
     
    # Append 0 to the first half
    for i in range(len(recAns)):
        s = recAns[i]
        mainAns.append("0" + s)
 
    # Append 1 to the second half
    for i in range(len(recAns) - 1, -1, -1):
        s = recAns[i]
        mainAns.append("1" + s)
 
    return mainAns
 
# Function to generate the
# Gray code of N bits
def generateGrayarr(n):
     
    arr = generateGray(n)
 
    # Print contents of arr
    print(*arr, sep = "\n")
 
# Driver Code
generateGrayarr(3)
 
# This code is contributed by avanitrachhadiya2155


C#
// Java program to generate
// n-bit Gray codes
using System;
using System.Collections.Generic;
class GFG {
 
  // This function generates all n
  // bit Gray codes and prints the
  // generated codes
  static List generateGray(int n)
  {
 
    // Base case
    if (n <= 0) {
      List temp = new List();
      temp.Add("0");
      return temp;
    }
    if (n == 1) {
      List temp = new List();
      temp.Add("0");
      temp.Add("1");
      return temp;
    }
 
    // Recursive case
    List recAns = generateGray(n - 1);
    List mainAns = new List();
 
    // Append 0 to the first half
    for (int i = 0; i < recAns.Count; i++) {
      String s = recAns[i];
      mainAns.Add("0" + s);
    }
 
    // Append 1 to the second half
    for (int i = recAns.Count - 1; i >= 0; i--) {
      String s = recAns[i];
      mainAns.Add("1" + s);
    }
    return mainAns;
  }
 
  // Function to generate the
  // Gray code of N bits
  static void generateGrayarr(int n)
  {
    List arr = new List();
    arr = generateGray(n);
 
    // print contents of arr
    for (int i = 0; i < arr.Count; i++)
    {
      Console.WriteLine(arr[i]);
    }
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    generateGrayarr(3);
  }
}
 
// This code is contributed by grand_master.


C++
// C++ implementation of the above approach
#include 
using namespace std;
 
void GreyCode(int n)
{
     // power of 2
    for (int i = 0; i < (1 << n); i++)
    {
        // Generating the decimal
        // values of gray code then using
        // bitset to convert them to binary form
        int val = (i ^ (i >> 1));
         
        // Using bitset
        bitset<32> r(val);
         
        // Converting to string
        string s = r.to_string();
        cout << s.substr(32 - n) << " ";
    }
}
 
 
// Driver Code
int main()
{
    int n;
    n = 4;
   
    // Function call
    GreyCode(n);
    
    return 0;
}


输出
000
001
011
010
110
111
101
100

时间复杂度: O(2 N )
辅助空间: O(2 N )

方法2:递归方法

想法是每次递归地附加位0和1,直到位数不等于N。

基本条件:此问题的基本情况是N的值等于0或1时。

递归条件:否则,对于大于1的任何值,递归地生成N – 1位的格雷码,然后为所生成的每个格雷码添加前缀0和1。

下面是上述方法的实现:

C++

// C++ program to generate
// n-bit Gray codes
 
#include 
using namespace std;
 
// This function generates all n
// bit Gray codes and prints the
// generated codes
vector generateGray(int n)
{
    // Base case
    if (n <= 0)
        return {"0"};
 
    if (n == 1)
    {
      return {"0","1"};
    }
 
    //Recursive case
    vector recAns=
          generateGray(n-1);
    vector mainAns;
     
    // Append 0 to the first half
    for(int i=0;i=0;i--)
    {
       string s=recAns[i];
       mainAns.push_back("1"+s);
    }
    return mainAns;
}
 
// Function to generate the
// Gray code of N bits
void generateGrayarr(int n)
{
    vector arr;
    arr=generateGray(n);
    // print contents of arr
    for (int i = 0 ; i < arr.size();
         i++ )
        cout << arr[i] << endl;
}
 
// Driver Code
int main()
{
    generateGrayarr(3);
    return 0;
}

Java

// Java program to generate
// n-bit Gray codes
import java.io.*;
import java.util.*;
class GFG
{
 
  // This function generates all n
  // bit Gray codes and prints the
  // generated codes
  static ArrayList generateGray(int n)
  {
 
    // Base case
    if (n <= 0)
    {
      ArrayList temp =
        new ArrayList(){{add("0");}};
      return temp;
    }
    if(n == 1)
    {
      ArrayList temp =
        new ArrayList(){{add("0");add("1");}};
      return temp;
    }
 
    // Recursive case
    ArrayList recAns = generateGray(n - 1);
    ArrayList mainAns = new ArrayList();
 
    // Append 0 to the first half
    for(int i = 0; i < recAns.size(); i++)
    {
      String s = recAns.get(i);
      mainAns.add("0" + s);
 
    }
 
    // Append 1 to the second half
    for(int i = recAns.size() - 1; i >= 0; i--)
    {
      String s = recAns.get(i);
      mainAns.add("1" + s);
    }
    return mainAns;
  }
 
  // Function to generate the
  // Gray code of N bits
  static void generateGrayarr(int n)
  {
    ArrayList arr = new ArrayList();
    arr = generateGray(n);
 
    // print contents of arr
    for (int i = 0 ; i < arr.size(); i++)
    {
      System.out.println(arr.get(i));   
    }
  }
 
  // Driver Code
  public static void main (String[] args)
  {
    generateGrayarr(3);
  }
}
 
// This code is contributed by rag2127.

Python3

# Python3 program to generate
# n-bit Gray codes
 
# This function generates all n
# bit Gray codes and prints the
# generated codes
def generateGray(n):
     
    # Base case
    if (n <= 0):
        return ["0"]
    if (n == 1):
        return [ "0", "1" ]
 
    # Recursive case
    recAns = generateGray(n - 1)
 
    mainAns = []
     
    # Append 0 to the first half
    for i in range(len(recAns)):
        s = recAns[i]
        mainAns.append("0" + s)
 
    # Append 1 to the second half
    for i in range(len(recAns) - 1, -1, -1):
        s = recAns[i]
        mainAns.append("1" + s)
 
    return mainAns
 
# Function to generate the
# Gray code of N bits
def generateGrayarr(n):
     
    arr = generateGray(n)
 
    # Print contents of arr
    print(*arr, sep = "\n")
 
# Driver Code
generateGrayarr(3)
 
# This code is contributed by avanitrachhadiya2155

C#

// Java program to generate
// n-bit Gray codes
using System;
using System.Collections.Generic;
class GFG {
 
  // This function generates all n
  // bit Gray codes and prints the
  // generated codes
  static List generateGray(int n)
  {
 
    // Base case
    if (n <= 0) {
      List temp = new List();
      temp.Add("0");
      return temp;
    }
    if (n == 1) {
      List temp = new List();
      temp.Add("0");
      temp.Add("1");
      return temp;
    }
 
    // Recursive case
    List recAns = generateGray(n - 1);
    List mainAns = new List();
 
    // Append 0 to the first half
    for (int i = 0; i < recAns.Count; i++) {
      String s = recAns[i];
      mainAns.Add("0" + s);
    }
 
    // Append 1 to the second half
    for (int i = recAns.Count - 1; i >= 0; i--) {
      String s = recAns[i];
      mainAns.Add("1" + s);
    }
    return mainAns;
  }
 
  // Function to generate the
  // Gray code of N bits
  static void generateGrayarr(int n)
  {
    List arr = new List();
    arr = generateGray(n);
 
    // print contents of arr
    for (int i = 0; i < arr.Count; i++)
    {
      Console.WriteLine(arr[i]);
    }
  }
 
  // Driver Code
  public static void Main(String[] args)
  {
    generateGrayarr(3);
  }
}
 
// This code is contributed by grand_master.

输出
000
001
011
010
110
111
101
100

时间复杂度: O(2 N )
辅助空间: O(2 N )

方法3 :(使用位集)

我们应该首先找到从1到n的二进制数,然后将其转换为字符串,然后使用字符串的子字符串函数将其打印出来。

下面是上述想法的实现:

C++

// C++ implementation of the above approach
#include 
using namespace std;
 
void GreyCode(int n)
{
     // power of 2
    for (int i = 0; i < (1 << n); i++)
    {
        // Generating the decimal
        // values of gray code then using
        // bitset to convert them to binary form
        int val = (i ^ (i >> 1));
         
        // Using bitset
        bitset<32> r(val);
         
        // Converting to string
        string s = r.to_string();
        cout << s.substr(32 - n) << " ";
    }
}
 
 
// Driver Code
int main()
{
    int n;
    n = 4;
   
    // Function call
    GreyCode(n);
    
    return 0;
}

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

时间复杂度: O(2 N )
辅助空间: O(N)