hdu 5676 ztr loves lucky numbers

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

看解题报告做的!
思路:用字符读入数据,对于长度为奇数的可以直间确定super lucky number.对于长度len是偶数的字串s,用4和7组合的字典序最小,长度为len的子串s2,然后不断求下一个字典序.在此过程中找到一个最小字典序的s2大于s的字典序,s2即为所求。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cstdio>
#include<cmath>
using namespace std;
char s[100],b[100];
int main()
{
     int t;
     scanf("%d",&t);
     while(t--)
     {
        scanf("%s",s);
        int len=strlen(s);

        if(strcmp(s,"0")==0)
        {

            printf("47\n");
            continue;
         }

        if(len%2)
        {
            for(int i=0;i<=len/2;i++)
                printf("4");
            for(int j=0;j<=len/2;j++)
            printf("7");
            printf("\n");
            continue;
         }

         for(int i=0;i<len/2;i++)
         b[i]='4';
         for(int i=len/2;i<len;i++)
         b[i]='7';
         b[len]=0;

         int flag=0;
         do{
            if(strcmp(b,s)>=0)
            {
                printf("%s\n",b);
                flag=1;
                break;
             }

         }while(next_permutation(b,b+len));

         if(!flag)
         {
            for(int i=0;i<=len/2;i++)
            printf("4");
            for(int j=0;j<=len/2;j++)
            printf("7");
            printf("\n");
         }
         }  
         return 0;
 } 

你可能感兴趣的:(hdu 5676 ztr loves lucky numbers)