格雷码生成算法
- 格式:pdf
- 大小:46.25 KB
- 文档页数:3
格雷码⽣成算法
在博客中看到过⼀次格雷码⽣成算法,我在这⾥也想写⼀下。
原⽂中的算法为:假设已经⽣成了k位格雷码,那么k+1位格雷码的⽣成⽅式为(1) 按序在k位格雷码前插⼊⼀位0,⽣成⼀组编码,(2)按逆序
在k位格雷码前插⼊⼀位1,⽣成另外⼀组编码,两组编码合起来就是k+1位格雷码。
如下例:
已有2位格雷码:00, 01, 11, 10,要⽣成3位格雷码,采⽤此算法:
(1)按序在各码前插⼊0,⽣成000,001, 011,010;
(2)按逆序在各码前插⼊1,⽣成110,111, 101,100;
(3)将两组编码组合起来:000, 001, 011, 010, 110, 111, 101, 100,为3位格雷码。
另外⼀种算法与此算法类似,不同的是插⼊的位是在格雷码的后⾯:
对于k位格雷码,在各格雷码后⾯分别插⼊0, 1 或 1, 0,⽣成两个编码,所有插⼊完成后组合起来的编码为k+1位格雷码。
如已有2位格雷码:00,01,11,10,⽣成3位格雷码,采⽤此算法:
(1)在00编码后⾯分别插⼊0,1,⽣成000, 001;
(2)在01编码后⾯分别插⼊1,0,⽣成011, 010;
(3)在11编码后⾯分别插⼊0,1,⽣成110, 111;
(4)在10编码后⾯分别插⼊1,0,⽣成101,100;
(5)将⽣成的编码组合起来:000, 001, 011, 010, 110, 111, 101, 100,为3位格雷码。
#include
#include
#include
#include
void GrayCodeOne(int num);
void GrayCodeTwo(int num);
using namespace std;
int main()
{
int count;
cout << "Input Code Number:";
cin >> count;
cout << "Produce Gray Code using method 1" << endl;
clock_t beginOne = clock();
GrayCodeOne(count);
clock_t endOne = clock();
cout << "Gray Code First Method using time: " << (endOne - beginOne) << endl;
cout << "Produce Gray Code using method 2" << endl;
clock_t beginTwo = clock();
GrayCodeTwo(count);
clock_t endTwo = clock();
cout << "Gray Code Second Method using time: " << (endTwo - beginTwo) << endl;
return 0;
}
// Method to produce gray code using method inserting 0 in front of old gray code by positive
// and inserting 1 in front of old gray code by nagative.
void GrayCodeOne(int num)
{
if (num < 1)
{
cout << "Error input Integer" << endl;
return;
}
vector
int cIdx = 1;
for (; cIdx <= num; cIdx++)
{
if (codeVec.size() < 2)
{
codeVec.push_back("0");
codeVec.push_back("1");
}
else
{
vector
tranVec.resize(2 * codeVec.size());
int tranIdx = 0;
vector
for (; codeIter != codeVec.end(); codeIter++)
{
string str = "0";
str.append(*codeIter);
tranVec[tranIdx++] = str;
}
vector
for (; rCodeIter != codeVec.rend(); rCodeIter++)
{
string str = "1";
str.append(*rCodeIter);
tranVec[tranIdx++] = str;
}
codeVec.assign(tranVec.begin(), tranVec.end());
}
}
//vector
//for (; vecIter != codeVec.end(); vecIter++)
//{
// cout << *vecIter << endl;
//}
return;
}
// Method to produce gray code using method inserting 0/1 in the back of first gray code
// then inserting 1/0 in the back of next gray code.
void GrayCodeTwo(int num)
{
if (num < 1)
{
cout << "Input error Integer" << endl;
return;
}
vector
int cIdx = 1;
for (; cIdx <= num; cIdx++)
{
if (codeVec.size() < 2)
{
codeVec.push_back("0");
codeVec.push_back("1");
}
else
{
vector
int tranIdx = 0;
int cIdx = codeVec.size();
tranVec.resize(2 * cIdx);
for (int vIdx = 0; vIdx < cIdx; vIdx++)
{
string str = codeVec[vIdx];
if (0 == (vIdx % 2))
{
string str0 = str;
str0.append("0");
tranVec[tranIdx++] = str0;
string str1 = str; str1.append("1");
tranVec[tranIdx++] = str1;
}
else
{
string str0 = str;
str0.append("1");
tranVec[tranIdx++] = str0;
string str1 = str;
str1.append("0");
tranVec[tranIdx++] = str1;
}
}
codeVec.assign(tranVec.begin(), tranVec.end());
}
}
//vector
//for (; vecIter != codeVec.end(); vecIter++)
//{
// cout << *vecIter << endl;
//}
return;
}
运⾏时间的测试:
12位格雷码,⽅法⼀和⽅法⼆所需时钟数
16位格雷码,两种⽅法所需时钟数