生成格雷码 C++

1、生成格雷码

#include 
#include
using namespace std;

#include 
#include 
#include 
 
using namespace std;
#define M_PI 3.14159265358979323846
typedef unsigned char uchar;


///产生n位格雷码(镜像排列方法构建)
vector generateGraycode(int n)
{
	vector nGraycode;
	if (n == 1)
	{
		nGraycode.push_back("0");
		nGraycode.push_back("1");
	}
	else
	{
		vector mid_ans = generateGraycode(n - 1);
		for (vector::iterator iter = mid_ans.begin(); iter != mid_ans.end(); ++iter)
		{
			string temp = *iter;
			temp.insert(0, "0");
			nGraycode.push_back(temp);
		}
		for (int i = mid_ans.size() - 1; i >= 0; --i)
		{
			string temp = mid_ans[i];

			temp.insert(0, "1");
			nGraycode.push_back(temp);
		}
	}
	return nGraycode;
}

int main()
{
    vectornGraycode;
	nGraycode = generateGraycode(4);
	for(int i = 0; i < nGraycode.size(); i++)
		cout << nGraycode.at(i) << endl;
	return 0;
}

2、相移数据验证

const float pi = M_PI;
   int _screenCols = 1080;
   int _GrayBit = 4;
   float T = 0.0;
   for (unsigned int i = 0; i < 1; i++) {
     float phase = 2.0 * pi / 4 * i;  // 初相
     T = (float)_screenCols / (float)pow(2, _GrayBit); // 周期
     std::cout<

你可能感兴趣的:(c++,算法,图论)