枚举_Lucky Number

source

Description

Petya 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 Petya came across a positive integer n. Help him to find the least super lucky number which is not less than n.

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

Output
Output the least super lucky number that is more than or equal to 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.

Input

4500

Output

4747

Input

47

Output

47

题意:super lucky number是只含有4,7且4的位数等于7的位数的数字;寻找大于等于n的super lucky number
题解:容易知道,因为4,7的位数相等,幸运数一定是偶数的。所以当num的位数n为奇数的时候,幸运数是位数为n+1位,左边(n+1)/2位为4,右边(n+1)/2位为7,可以保证为符合题意最小的幸运数。num的位数为偶数时,构造左边n/2位为4,右边n/2位为7的幸运数,然后通过next_permutation找到第一个最小的幸运数。

#include
#include
#include
using namespace std;

int main()
{
    char num[50],cpy[50];
    scanf("%s",num);
    int len=strlen(num);
    bool unreal=true;
    if(len&1)
    {
        for(int i=0;i<=len>>1;i++)
        {
            printf("4");

        }
        for(int i=0;i<=len>>1;i++)
        {
            printf("7");

        }
    }
    else
    {
        for(int i=0;i>1;i++)
        {
                cpy[i]='4';
        }
        for(int i=len>>1;i=0)
                {
                    printf("%s\n",cpy);
                    unreal=false;
                    break;
                }
        }
        while(next_permutation(cpy,cpy+len));
        if(unreal)
        {
            for(int i=0;i<=len>>1;i++)
            {
                printf("4");

            }
            for(int i=0;i<=len>>1;i++)
            {
                printf("7");

            }

        }

    }

}

你可能感兴趣的:(枚举_Lucky Number)