数据量很少,据说暴搜也可以AC的,好一点的话可以考虑二分。我求写代码方便用了排列的方法:因为每一个幸运数都必然是2、4、6、8、10位的数,而不同位数的排列最小的那个也可知,就是47、4477、444777、44447777、4444477777。而因为输入的数子最大是10的9次方,10位的数一个就足够,其他的只要每个都依次往后产生比它小的字典序数字列,存进一个set容器内(要long long类型)。然后对于每个输入的数从set里面找出不小于当前数的那个数输出即可。
Run Time: 0sec
Run Memory: 312KB
Code length: 697Bytes
Submit Time: 2011-12-14 00:53:25
// Problem#: 4424
// Submission#: 1074843
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
int T, N;
int num[ 10 ];
set<long long> lucky;
long sum;
int n, i;
for ( n = 2; n <= 8; n += 2 ) {
for ( i = 0; i < n; i++ )
num[ i ] = ( i < n / 2 ? 4: 7 );
do {
sum = 0;
for ( i = n - 1; i >= 0; i-- )
sum = sum * 10 + num[ i ];
lucky.insert( sum );
} while ( next_permutation( num, num + n ) );
}
lucky.insert( 4444477777 );
cin >> T;
while ( T-- ) {
cin >> N;
cout << *lucky.lower_bound( N ) << endl;
}
return 0;
}