码迷,mamicode.com
首页 > 其他好文 > 详细

Blacklist

时间:2014-08-05 03:02:08      阅读:177      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   for   art   

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

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

简单题可是别想复杂了

bubuko.com,布布扣
#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

 

Blacklist,布布扣,bubuko.com

Blacklist

标签:style   blog   http   color   os   io   for   art   

原文地址:http://www.cnblogs.com/Mathics/p/3891336.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!