您的位置:首页 > 其它

格雷码的递归输出

2015-09-08 13:53 489 查看
//第一种

#include <math.h>

#include "stdlib.h"

#include "stdio.h"

void CreateGrayCode(int n,int x) //x初始值为0

{

if(x<pow(2,n))

{

int graycode=(x^(x>>1));

char buf[32];

printf("%032s\n",itoa(graycode,buf,2));

CreateGrayCode(n,x+1);

}

}

int main()

{

CreateGrayCode(10,0);

return 0;

}

//第二种

#include <iostream>

#include <math.h>

#include <vector>

#include <string>

using namespace std;

vector<string> getGray(int n) {

// write code here

int N=pow(2,n);

static int i=0;

static vector<string> vc;

if(i<N)

{

char temp[256];

sprintf(temp,"%d",i^(i>>1));

vc.push_back(temp);

++i;

getGray(n);

}

return vc;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: