Lucky Numbers (easy) CodeForces - 96B 全排列

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 and5, 17, 467 are not.

Lucky number is super lucky if it's decimal representation contains equal amount of digits4 and 7. For example, numbers47, 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 thann.

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.

Example
Input
4500
Output
4747
Input
47
Output
47



 

应用  next_permutation函数,可以把n中元素共n!中不同的排列生成出来。

关于next_permutation函数的介绍http://blog.csdn.net/liluoyu_1016/article/details/77154760

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
char a[15],b[15];
int main()
{
    while(cin>>a)
    {
        int len =strlen(a);
        if(len&1)
        {
            for(int i=0;i<=len/2;i++)
                cout<<"4";
            for(int i=0;i<=len/2;i++)
                cout<<"7";
            cout<<endl;
            continue;
        }
        int i;
        for(i=0;i<len/2;i++)
            b[i]='4';
        for(i=len/2;i<len;i++)
            b[i]='7';
        b[i]='\0';
        int f=0;
        do
        {
            if(strcmp(a,b)<=0)
            {
                cout<<b<<endl;
                f=1;
                break;
            }
        }while(next_permutation(b,b+len));
        if(f==0)
        {
            for(i=0;i<=len/2;i++)
                cout<<"4";
            for(i=0;i<=len/2;i++)
                cout<<"7";
            cout<<endl;
        }
    }
    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值