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 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 Petya came across a positive integer n. Help him to find the least super lucky number which is not less than n.
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.
Examples
Input
4500
Output
4747
Input
47
Output
47
题意:超级幸运数字是只由数字4和7构成并且4和7的个数相同。题目要求输入一个数n,你要找出一个不小n的超级幸运数
思路:分两种情况 1.当给所数n的位数为奇数时,那么只需输出位数为n的位数+1的最小超级幸运数
2.当给所数n的位数为偶数时,用相同位数的最大超幸运数和它比较,若仍小于n,则输出位数为n的位数+2的最小超级幸运数。若大于n,则将相同位数的最小超级幸运数通过全排列找出等于n或大于n的超级幸运数输出
代码:
#include<bits/stdc++.h>
using namespace std;
void print(int len)
{
int i;
for(i=1;i<=len;i++)
printf("4");
for(i=1;i<=len;i++)
printf("7");
}
int main()
{
char s[15],n[15];
int i,len,r;
while(~scanf("%s",s))
{
len=strlen(s);
if(len%2==1)
{
print((len+1)/2);
continue;
}
for(i=0;i<len/2;i++)
n[i]='7';
for(i=len/2;i<len;i++)
n[i]='4';
r=strcmp(s,n);
if(r==1)
print((len+2)/2);
else{
for(i=0;i<len/2;i++)
n[i]='4';
for(i=len/2;i<len;i++)
n[i]='7';
do{
r=strcmp(s,n);
if(r==0||r==-1)
{
for(i=0;i<len;i++)
printf("%c",n[i]);
break;
}
}while(next_permutation(n,n+len));
}
printf("\n");
}
return 0;
}