格雷码的递归输出
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;
}
#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;
}
相关文章推荐
- 基于STM32旋转编码器
- Matlab 仿真实现TI Instaspin 的Foc 逆Clarke变换和SVPWM
- 全球顶级应用性能监控管理服务商分析
- Matrix Factorization, Algorithms, Applications, and Avaliable packages
- Quality Center配置邮箱服务
- ARRAYLIST VECTOR LINKEDLIST 区别与用法
- PHP操作MySQL数据库
- windbg常用命令
- 实现一个日期选择插件
- Android最佳性能实践(一)——合理管理内存
- 关于iOS TDD&BDD的学习与使用
- 歪果仁眼中的中国科技界必须知道的7件事
- 一个项目经理的个人体会、经验总结(转)
- Hibernate 执行SQL语句
- WPF之VirtualizingStackPanel.IsVirtualizing="False"
- 算法
- 进程/线程同步——Critical Section,Mutex,Semaphore,Event区别
- unity警告“Tiled GPU perf. warning: RenderTexture color surface (0x0) was not cleared/discarded, doin”
- ubuntu 14.04下安装firfox,chromium,chrome可用的flash
- Unity Application