欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

hdu 5676 ztr loves lucky numbers(BC——暴力打表+二分查找)

程序员文章站 2022-03-26 09:28:25
题目链接: ztr loves lucky numbers time limit: 2000/1000 ms (java/others)memory limit: 65536/65536 k (...

题目链接:

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 integern(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 (p.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;i777777777444444444ll)
        {
            printf("44444444447777777777\n");
            continue;
        }
        int l=0,r=k;
        while (r>l)
        {
            int mid=(l+r)/2;
           // cout