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 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.
Sample Input
Input
4500
Output
4747
Input
47
Output
47
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
int b[6] = {4,4,4,7,7,7};
int c[8] = {4,4,4,4,7,7,7,7};
int f[10];
void init()
{
f[0] = 1;
for(int i = 1;i<=9;i++)
{
f[i] = 10*f[i-1];
}
}
ll num[200]={47,74,4477,4747,4774,7447,7474,7744};
int main()
{
init();
int i;
int k = 8;
do{
for(i=0;i<6;i++)
{
num[k] += b[i]*f[5-i];
}
k++;
}while(next_permutation(b,b+6));
do{
for(i=0;i<8;i++)
{
num[k] += c[i]*f[7-i];
}
k++;
}while(next_permutation(c,c+8));
num[k] = 4444477777;
/*for(i=0;i<=k;i++)
cout << num[i] << endl;*/
//cout << "Hello world!" << endl;*/
/*for(i=0;i<10;i++)
cout<<f[i] << endl;*/
int n;
while(cin>>n)
{
for(i=0;;i++)
{
if(num[i]>=n)
{
cout << num[i] <<endl;
break;
}
}
}
return 0;
}