題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=5676
ztr loves lucky numbers
Time Limit: 2000/1000 MS (Java/Others)Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 594Accepted Submission(s): 257
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
(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.
Output
For each cases
Output the answer
Sample Input
2
4500
47
Sample Output
4747
47
Source
BestCoder Round #82 (div.2)
題目大意:
ztr喜歡幸運數字,他對於幸運數字有兩個要求
1:十進制表示法下只包含4、7
2:十進制表示法下4和7的數量相等
比如47,474477就是
而4,744,467則不是
現在ztr想知道最小的但不小於n的幸運數字是多少
解題思路:暴力打出所有幸運數~~~~然後二分查找即可。
詳見代碼。
#include
#include
using namespace std;
#define ll long long
ll a[100000];
int k=0;
void dfs(ll ans,int num4,int num7)
{
if (num4==0&&num7==0)
{
a[k++]=ans;
return;
}
if (num4==0)
{
dfs(ans*10+7,num4,num7-1);
}
else if (num7==0)
{
dfs(ans*10+4,num4-1,num7);
}
else
{
dfs(ans*10+4,num4-1,num7);
dfs(ans*10+7,num4,num7-1);
}
}
int main()
{
int t;
scanf("%d",&t);
for (int i=2;i<=18;i+=2)
dfs(0,i/2,i/2);
while (t--)
{
ll n;
scanf("%lld",&n);
if(n>777777777444444444LL)
{
printf("44444444447777777777\n");
continue;
}
int l=0,r=k;
while (r>l)
{
int mid=(l+r)/2;
// cout<