生成格雷码_生成格雷码序列

本文介绍了如何生成n位格雷码序列的算法,该算法基于递归思想,通过在n-1位格雷码的基础上添加0或1前缀形成完整的n位序列。详细步骤包括创建两个列表,分别为所有元素添加0和1前缀,然后将这两个列表连接起来,形成新的n位格雷码列表。示例和C++实现进一步解释了这一过程。
摘要由CSDN通过智能技术生成

生成格雷码

Problem statement:

问题陈述:

Given a number N, write a function which generates all n-bit gray code sequences, a gray code sequence is a sequence such that successive patterns in it differ by one bit.

给定数字N ,编写一个生成所有n位格雷码序列的函数,格雷码序列是这样的序列,即其中的连续图案相差一位。

Example:

例:

    Input:
    N=2
    Output:
    00 01 11 10

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

Solution:

解:

Construction an n bit gray code follows a robust algorithm. The binary-reflected Gray code list for n bits can be generated recursively from the list for n − 1 bits by reflecting the list (i.e. listing the entries in reverse order), prefixing the entries in the original list with a binary 0, prefixing the entries in the reflected list with a binary 1, and then concatenating the original list with the reversed list.

构造n位格雷码遵循稳健算法。 可以通过反射列表(即以相反顺序列出条目),在原始列表中的条目前面加上二进制0,在列表中添加前缀n,从而从n-1位的列表中递归生成n位的二进制反射格雷码列表。反映列表中带有二进制1的条目,然后将原始列表与反向列表连接起来。

Reference: Gray_code

参考: Gray_code

生成n位格雷码列表的算法 (Algorithm to generate n bit gray code list)

  1. Let, the n-1 length gray code list has m elements, (m=2^(n-1))

    假设n-1个长度的格雷码列表包含m个元素(m = 2 ^(n-1))

  2. Create list1 by adding prefix 0 to all the m elements

    通过将前缀0添加到所有m个元素来创建list1

  3. Create list2 by adding prefix 1 to all the m elements

    通过将前缀1添加到所有m个元素来创建list2

  4. New list for n-bit gray code= list1+reverse(list)

    n位格雷码的新列表 = list1 + reverse(list)

  5. New list size 2m (2^n)

    新清单大小2m(2 ^ n)

For any n bit gray code list, we start with gray code list of length 1 (elements are only '0' and '1'). Then iterate till finding n-bit gray code list.

对于任何n位格雷码列表 ,我们从长度为1的格雷码列表开始(元素仅是“ 0”和“ 1”)。 然后迭代直到找到n位格雷码列表。

The above can be explained with help of an example,

以上内容可以通过示例进行说明,

    Let we need to find gray code sequence for n=3
    //Base case
    For n=1
    List: 0, 1

    For n=2
    Add prefix '0' to create list1
    List1= 00, 01
    Add prefix '1' to create list2
    List2= 10, 11
    New list=list1 + reverse(list2)
    =00, 01, 11, 10 //List for n=2

    For n=3
    Add prefix '0' to create list1
    List1= 000, 001, 011, 010
    Add prefix '1' to create list2
    List2= 100, 101, 111, 110
    New list=list1 + reverse(list2)
    =000, 001, 011, 010, 110, 111, 101, 100 //List for n=3


C++ implementation

C ++实现

#include <bits/stdc++.h>
using namespace std;

string get_string(char c)
{
	string s(1,c);
	return s;
}

void generateCode(int n)
{
	if(n<=0){
		cout<<"invalid input\n";
		return;
	}

	vector<string> a,temp;
	a.push_back("0");
	a.push_back("1");

	for(int i=0;i<n-1;i++){
		for(int j=0;j<a.size();j++){
			temp.push_back(get_string('0')+a[j]);
		}
		for(int j=a.size()-1;j>=0;j--){
			temp.push_back(get_string('1')+a[j]);
		}
		a=temp;
		temp.clear();
	}

	for(int i=0;i<a.size();i++)
		cout<<a[i]<<"\n";
}

int main(){
	int n;
	
	cout<<"Enter n:\n";
	cin>>n;
	
	cout<<"Printing gray codes for "<<n<<" bit\n";
	generateCode(n);

	return 0;
}

Output

输出量

First run:
Enter n:
3
Printing gray codes for 3 bit
000
001
011
010
110
111
101
100


Second run:
Enter n:
4
Printing gray codes for 4 bit
0000
0001
0011
0010
0110
0111
0101
0100
1100
1101
1111
1110
1010
1011
1001
1000 


翻译自: https://www.includehelp.com/icp/generate-gray-code-sequences.aspx

生成格雷码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值