您的位置:首页 > 其它

hdu 5676 ztr loves lucky numbers 打表+二分

2017-05-08 17:47 316 查看

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


[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
There are T(1≤n≤105) cases

For each cases:

The only line contains a positive integer n(1≤n≤1018). This number doesn't have leading zeroes.

[align=left]Output[/align]
For each cases
Output the answer

[align=left]Sample Input[/align]

2
4500
47

[align=left]Sample Output[/align]

4747
47

[align=left]Source[/align]
BestCoder Round #82 (div.2)
思路:数总共没多少,打表即可,再最大的情况特判一下;

#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\n");
else printf("%lld\n",*it);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: