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.
The only line contains a positive integer n (1 ≤ n ≤ 109). This number doesn't have leading zeroes.
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.
4500
4747
47
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;
}