n位格雷码

解题思路
若某一位上数字和上一位相等,那这一位就是 1 1 1 ,否则是 0 0 0 ,就可以用亦或来做。
code
#include<iostream>
#include<cstdio>
#define int long long
using namespace std;
int n;
void output(int x)
{
int a[20],tot=0;
while(x) a[++tot]=x%2,x>>=1;
for(int i=n;i>tot;i--) printf("0");
while(tot) printf("%lld",a[tot--]);
printf("\n");
}
signed main()
{
cin>>n;
for(int i=0;i<(1<<n);i++)
output(i^(i>>1));
}
336

被折叠的 条评论
为什么被折叠?



