CodeForces - 96B

Petya喜欢幸运数字。 大家都知道,正数整数是幸运的,如果它们的十进制表示不包含4和7以外的数字。例如,数字47,744,4是幸运的,5,17,46,7不是。 幸运数字超级幸运,如果它的十进制表示包含相等数量的数字4和7.例如,数字47,7744,474477是超级幸运,4,744,467不是。 有一天,Petya遇到了一个正整数n。 帮助他找出大于等于给定的数字的最小的超级幸运数字.

Input

输入一个正整数n(n<1e9)。

Output

输出大于等于给定的数字的最小的超级幸运数字.n.

Please, do not use the %lld specificator to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specificator.

Example
Input

4500

Output

4747

Input

47

Output
47

这道题用dfs来做,dfs时先让末尾加上4,继续搜索,找不到把4变成7(cnt1不变即可).直到找到符合条件的数字直接退出即可,由于搜索的顺序性,找到的第一个数就是答案.
代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int pp=0,n;
void dfs(int cnt1,int cnt2,int len,ll number)
{
    if(cnt1==len&&cnt2==len)
    {
        if(number>=n)
        {cout<<number<<endl;pp=1;exit(0);}
        return;
    }
    if(cnt1<len)
        dfs(cnt1+1,cnt2,len,number*10+4);
    if(cnt2<len)
        dfs(cnt1,cnt2+1,len,number*10+7);
}
int main()
{
    cin>>n;
    pp=0;
    for(int i=1;i<=9;i++)
    {
        if(pp==1)
            break;
        dfs(0,0,i,0);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值