趁着复习把课上的作业和代码上传上来嘿嘿嘿,这个系列的代码都是自己写的或者老师教学用的可能内存管理上有点小瑕疵,但是算法理解还是不错的!本系列所有代码在dev-cpp上可以跑通~
gray码:
先来看一下gray码的定义把,
然后递归实现:
输入n,输出n位gray码
#include <stdio.h>
#include <stdlib.h>
void graycode(int n, int a[]){
if(n==1){
a[0]=0;
a[1]=1;
}
else{
gray(n-1,a[]);
int L=1<<(n-1);
for(int i=0;i<L;i++){
a[2*L-i-1]=a[i]+L;
}
}
}
void display(int n,int G[]){
int m = 1<<n;
int s =0 ;