本文主要是介绍【51nod】3395 n位格雷码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
n位格雷码
Link
解题思路
若某一位上数字和上一位相等,那这一位就是 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));
}
这篇关于【51nod】3395 n位格雷码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!