hdu 5676 ztr loves lucky numbers 打表+二分

ztr loves lucky numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1834    Accepted Submission(s): 707


Problem Description
ztr loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

Lucky number is super lucky if it's decimal representation contains equal amount of digits 4 and 7. For example, numbers 47, 7744, 474477 are super lucky and 4, 744, 467 are not.

One day ztr came across a positive integer n. Help him to find the least super lucky number which is not less than n.
 
Input
There are T(1n105) cases

For each cases:

The only line contains a positive integer n(1n1018). This number doesn't have leading zeroes.
 
Output
For each cases
Output the answer
 
Sample Input
2 4500 47
 
Sample Output
4747 47
 
Source

 思路:数总共没多少,打表即可,再最大的情况特判一下;

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e2+10,M=1e6+10,inf=1e9+10;
const ll INF=5e17+10,mod=1e9+7;

///数组大小
int check(ll x)
{
    int s=0,q=0;
    while(x)
    {
        if(x%10==4)s++;
        else q++;
        x/=10;
    }
    if(s==q)return 1;
    return 0;
}
vector<ll>ans;
vector<ll>::iterator it;
void dfs(ll x)
{
    if(x>INF)return;
    if(check(x))ans.push_back(x);
    dfs(x*10+4);
    dfs(x*10+7);
}
int main()
{
    dfs(4),dfs(7);
    sort(ans.begin(),ans.end());
    int T;
    scanf("%d",&T);
    while(T--)
    {
        ll x;
        scanf("%lld",&x);
        it=lower_bound(ans.begin(),ans.end(),x);
        if(it==ans.end())
            printf("44444444447777777777
");
        else printf("%lld
",*it);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/jhz033/p/6826228.html