hdu 5676 ztr loves lucky numbers(STL大法好)

hdu 5676 &&BestCoder Round #82 div2 1002 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 (1n105) cases

For each cases:

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

Output
For each cases
Output the answer
 

Sample Input

2 4500 47
 

Sample Output

4747 47
solution:
 用next_permutation这道题就变成了水题,这是一个求一个排序的下一个排列的函数,可以遍历全排列,因此我们只需再用strcmp比较即可,具体看代码~真是长了一个大姿势。
#include
#include
#include
using namespace std;
char b[50],c[50];
int main()
{
    int t;
    scanf("%d", &t);
    while (t--)
    {
        scanf("%s", b);
        int len = strlen(b);
        if (len & 1)
        {
            for (int i = 0; i <= len / 2; i++)printf("4");
            for (int i = 0; i <= len / 2; i++)printf("7");
            printf("\n"); continue;
        }
        for (int i = 0; i < len / 2; i++)c[i]='4';
        for (int i = len/2; i < len; i++)c[i]='7';
        c[len] = 0;
        int f = 0;
        do
        {
            if (strcmp(b, c) <= 0)
            {
                printf("%s\n", c);
                f = 1; break;
            }
        } while (next_permutation(c, c + len));
        if (f==0)
        {
            for (int i = 0; i <= len / 2; i++)printf("4");
            for (int i = 0; i <= len / 2; i++)printf("7");
            printf("\n"); 
        }
    }
    return 0;
}



你可能感兴趣的:(HDU,杂题,其他)