2177: Lucky Numbers (easy)
Time Limit: 2 Sec Memory Limit: 256 MBSubmit: 45 Solved: 22
[ Submit][ Status][ Web Board]
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.
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
题意:求不小于n数,这个数是4 和 7 组成,且4和7的个数相同。
解析:题目中n给的10的9次,可以想到满足 super lucky number的数不多,暴力求解。最大也就4444477777;
这题在网上看到别人用字典序排序写的,比我这个暴力的好很多,毕竟要自己一个个写出来。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
ll a[1000]={
47,74,//2
4477,4747,4774,7447,7474,7744,//4
444777,447477,447747,447774,474477,474747,474774,477447,477474,477744,
744477,744747,744774,747447,747474,747744,774447,774474,774744,777444,//6
44447777,44474777,44477477,44477747,47447774,47474477,47474747,47474774,47477447,47477474,47477744,47744477,47744747,47744774,
47747447,47747474,47747744,47774447,47774474,74774474,74774744,74777444,77444477,77444747,77444774,77447447,77447474,77447744,77474447,77474474,77474744,
77477444,77744447,77744474,77744744,77747444,77774444,//8
4444477777//10
};
ll n;
while(~scanf("%lld",&n))
{
for(int i=0;;i++)
if(a[i]>=n)
{
printf("%lld\n",a[i]);
break;
}
}
return 0;
}