您的位置:首页 > 其它

HDU 5676 贪心

2016-05-17 20:10 316 查看
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn = 1E6 + 10;
long long res[maxn], n, len, T;
void dfs(int a, int b, long long x)
{
if (a == b && x) res[len++] = x;
if (a < 9) dfs(a + 1, b, x * 10 + 4);
if (b < 9) dfs(a, b + 1, x * 10 + 7);
}
int main(int argc, char const *argv[])
{
dfs(0, 0, 0);
sort(res, res + len);
scanf("%lld", &T);
while (T--)
{
scanf("%lld", &n);
long long ans = lower_bound(res, res + len, n) - res;
if (ans < len) printf("%lld\n", res[ans]);
else printf("44444444447777777777\n");
}
return 0;
}


按位DFS,每一位只可能是4或7且4的个数与7的个数相等。

注意边界条件,10个4和10个7时超过ull,特判后输出。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: