#include
using namespace std;
unsigned int a[1024];
void gray(int n)
{
if(n==1)
{
a[1] = 0;
a[2] = 1;
}
else
{
gray(n-1);
unsigned int k=1<<(n-1), i=k;
for(; i>0; i–)
a[2*k-i+1] = a[i] + k;
}
}
void out(int n)
{
char str[15];
int m = 1<<n;
for(int i=1; i<=m; i++)
{
itoa(a[i], str, 2);
int strlength = strlen(str);
for(int j=0; j<n-strlength; j++)
cout << ‘0’;
cout << str << endl;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int n = 8;
gray(n);
out(n);
system(“pause”);
return 0;
}