在博客中看到过一次格雷码生成算法,我在这里也想写一下。
原文中的算法为:假设已经生成了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 <iostream> #include <vector> #include <string> #include <time.h> 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<string> codeVec; int cIdx = 1; for (; cIdx <= num; cIdx++) { if (codeVec.size() < 2) { codeVec.push_back("0"); codeVec.push_back("1"); } else { vector<string> tranVec; tranVec.resize(2 * codeVec.size()); int tranIdx = 0; vector<string>::iterator codeIter = codeVec.begin(); for (; codeIter != codeVec.end(); codeIter++) { string str = "0"; str.append(*codeIter); tranVec[tranIdx++] = str; } vector<string>::reverse_iterator rCodeIter = codeVec.rbegin(); for (; rCodeIter != codeVec.rend(); rCodeIter++) { string str = "1"; str.append(*rCodeIter); tranVec[tranIdx++] = str; } codeVec.assign(tranVec.begin(), tranVec.end()); } } //vector<string>::iterator vecIter = codeVec.begin(); //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<string> codeVec; int cIdx = 1; for (; cIdx <= num; cIdx++) { if (codeVec.size() < 2) { codeVec.push_back("0"); codeVec.push_back("1"); } else { vector<string> tranVec; 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<string>::iterator vecIter = codeVec.begin(); //for (; vecIter != codeVec.end(); vecIter++) //{ // cout << *vecIter << endl; //} return; }
运行时间的测试:
12位格雷码,方法一和方法二所需时钟数
16位格雷码,两种方法所需时钟数
代码如下:
---------------------
作者:aitazhixin
来源:CSDN
原文:https://blog.csdn.net/aitazhixin/article/details/61915679
版权声明:本文为博主原创文章,转载请附上博文链接!