HDU 5756 ztr loves lucky numbers (dfs)(搜索)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5676

题目大意:只有7和4且7和4数量相等的数字为幸运数字,给你数字n,你要找出大于等于n的最小的幸运数字

解题思路:
计算可得幸运数字最多有C21+C42+C63+…+C189<2^18,所以可以通过dfs打表列出所有的幸运数字,因为n最多有19位,所以要进行特判,如果大于7777777744444444,则必须输出十个四,十个七

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <numeric>
#include <functional>
#define RI(N) scanf("%d",&(N))
#define RII(N,M) scanf("%d %d",&(N),&(M))
#define RIII(N,M,K) scanf("%d %d %d",&(N),&(M),&(K))
#define mem(a) memset((a),0,sizeof(a))
using namespace std;
const int inf=1e9;
const int inf1=-1*1e9;
double EPS=1e-10;
typedef long long LL;
vector<LL> ans;

void dfs(int four,int seven,LL now)
{
    if(four&&four==seven) ans.push_back(now);
    if(four<9) dfs(four+1,seven,now*10+4);
    if(seven<9) dfs(four,seven+1,now*10+7);
    return ;
}

int main()
{
    dfs(0,0,0);
    sort(ans.begin(),ans.end());
    int t;
    RI(t);
    while(t--)
    {
        bool mark=false;
        char a[30];
        scanf("%s",a);
        int alen=strlen(a);
        if(alen==18)
        {
            for(int i=0;i<9;i++)
            {
                if(a[i]-'0'>7) mark=true;
            }
            for(int i=9;i<18;i++)
            {
                if(a[i]-'0'>4) mark=true;
            }
        }
        if(mark||alen==19)
        {
        printf("44444444447777777777\n");
        }
        else
        {
            LL x=0;
            for(int i=0;i<alen;i++)
            {
                x=x*10+a[i]-'0';
            }
            //cout<<x<<endl;
            cout<<*(lower_bound(ans.begin(),ans.end(),x))<<endl;;

        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值