1.链接地址
https://vjudge.net/problem/POJ-1401
2.问题描述
For example, they defined the function Z. For any positive integer N, Z(N) is the number of zeros at the end of the decimal form of number N!. They noticed that this function never decreases. If we have two numbers N1 < N2, then Z(N1) <= Z(N2). It is because we can never "lose" any trailing zero by multiplying by any positive number. We can only get new and new zeros. The function Z is very interesting, so we need a computer program that can determine its value efficiently.
输入样例
6 3 60 100 1024 23456 8735373
输出样例
0 14 24 253 5861 2183837
3.解题思路
这道题的思路是从1到n的数中有几个5的倍数,如果是10、20、30等等这些数,因子中只有一个5,所以算一次。如果是25,50,75等等这些数,因子中有5的平方,所以算两次。依此规律,125,625等等同样可以这样算。
4.算法实现源代码
#include<cstdio> #include<cstdlib> #include<iostream> using namespace std; int s[20]; int main() { int i,j,n,k,l; int ss,ans; scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&k); ans=0; ss=5; while(ss<=k) { ans+=k/ss; ss*=5; } printf("%d\n",ans); } return 0; }
博客围绕POJ-1401问题展开,该问题定义函数Z,求正整数N的阶乘N!末尾零的个数。解题思路是统计1到n中5的倍数个数,不同次幂的5倍数计算次数不同。还给出了算法实现源代码的转载地址。
2904

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



