P5657 [CSP-S2019] 格雷码

>>n+1位的grey码生成方法 n位  (0)正序排列+(1)反序排列 

计算第k位 -> k的二进制数 当前位与左边一位的^

//加优化的cout+string 不可以 
#include<cstdio>
#include<iostream>
#include<algorithm>
//#include<queue>
//#include<vector>
//#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define ddd printf("-----------------------\n");
using namespace std;
const int maxn=1e2+10 ;

ull n,k,tmp[maxn],cnt;
string ans;

int main()
{
    //ios::sync_with_stdio(false);
    cin>>n>>k;
    while(k){
        tmp[++cnt]=k%2;
        k>>=1;
    }
    for(int i=1;i<=n;i++){
        if(tmp[i+1]==tmp[i]) ans[i]='0';
        else ans[i]='1';
    }
    for(int i=n;i>=1;i--) cout<<ans[i];
    cout<<'\n';
    
    return 0;
}

 

posted @ 2023-09-03 09:26  JMXZ  阅读(40)  评论(0)    收藏  举报