Gray code by orange1122

#include<stdio.h>\x0d
#include<math.h>\x0d
int main(){\x0d
\x09int n,i,j,k,a[20];scanf("%d",&n);printf("Gray code  n=%d\n",n);for(i=0;i<pow(2,n);i++){k=i;for(j=0;j<n;j++){a[j]=k%2;k/=2;}for(k=1;k<n;k++){if(a[k]==1&&a[k-1]==1)a[k-1]=0;else if(a[k]==1&&a[k-1]==0)a[k-1]=1;}for(k=j-1;k>=0;k--){printf("%d",a[k]);}printf("\n");}}

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page