首页 > 代码库 > Blacklist

Blacklist

题意: 给出一个16进制数(long long),找出比他大的第一个16进制数

搜索题,看似水,却是水。

简单题可是别想复杂了

#include <cstdio>#include <cstring>char ch[20];int a[20];bool pd[20];int len;bool dfs(int p, bool boom){    if(p==len) return 1;    int t=a[p];    if(boom) t=0;    for(int i=t; i<16; i++)        if(!pd[i])        {            pd[i]=1;            a[p]=i;            if(dfs(p+1,boom|i>t)) return 1;            pd[i]=0;        }    return 0;}void print(){    for(int i=0; i<len; i++)        if(a[i]<=9) printf("%d",a[i]);        else printf("%c",a[i]-10+A);}int main(){    unsigned long long n;    scanf("%llX",&n);    sprintf(ch,"%llX",++n);    len=strlen(ch);    for(int i=0; i<len; i++)        if(ch[i]>=0&&ch[i]<=9) a[i]=ch[i]-0;        else a[i]=ch[i]-A+10;    if(dfs(0,0)) print();    else    {        memset(a,0,sizeof(a));        len++;        a[0]=1;        pd[1]=1;        dfs(1,1);        print();    }    return 0;}
View Code