C++题目 结尾0的个数
思路
思路很简单,就是用你二分答案的方法找答案
条件
int fast(int n)
{
int ret = 0;
while (n > 0)
{
ret += n / 5;
n /= 5;
}
return ret;
}
fast(中间值) >= Q
有了这个条件就很容易写了
因为题目求的是找到一个最小的N, 使得1×2×3…×N1×2×3…×N 的末尾恰好有Q个0
所以我们可以用暴力枚举的方式
帮你们试了
时间超限 56
所以说,只能用二分答案的方法
没学过二分答案的可以看这篇博客:二分答案——入门篇
代码
#include <bits/stdc++.h>
using namespace std;
int fast(int n)
{
int ret = 0;
while (n > 0)
{
ret += n / 5;
n /= 5;
}
return ret;
}
int main()
{
int t;
cin >> t;
int now = 1;
while(t--)
{
long long Q;
cin >> Q;
int l = 1;
int r = 1000000000;
int best = -1;
while (l <= r)
{
int mid = (l + r) / 2;
if (fast(mid) >= Q)
{
best = mid;
r = mid - 1;
}
else
{
l = mid + 1;
}
}
if (fast(best) == Q)
{
printf("Case %d: %d\n", now, best);
}
else
{
printf("Case %d: impossible\n", now);
}
now++;
}
return 0;
}
297

被折叠的 条评论
为什么被折叠?



