[CSP day1T1]格雷码

格雷码

题解

很简单的题,按题目给的方法模拟一遍,注意,要开unsigned long long。令笔者记忆深刻!!!

源码

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef unsigned long long LL;
LL pow_2[100];
template<typename _T>
void read(_T &x)
{
    _T f=1;x=0;char s=getchar();
    while(s>'9'||s<'0'){if(s=='-') f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=(x<<3)+(x<<1)+(s^48);s=getchar();}
    x*=f;
}
void solve(LL n,LL k)
{
    if(n==0LL) return ;
    if(k>pow_2[n-1LL])
    {
        printf("1");
        solve(n-1LL,pow_2[n-1LL]-(k-pow_2[n-1LL])+1LL);
    }
    else
    {
        printf("0");
        solve(n-1LL,k);
    }
}
int main()
posted @ 2019-11-18 21:11  StaroForgin  阅读(8)  评论(0)    收藏  举报  来源