http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=118
n!后面有多少个0
Problem:118
Time Limit:1000ms
Memory Limit:65536K
Description
从输入中读取一个数n,求出n!中末尾0的个数。
Input
输入有若干行。第一行上有一个整数m,指明接下来的数字的个数。然后是m行,每一行包含一个确定的正整数n,1<=n<=1000000000。
Output
对输入行中的每一个数据n,输出一行,其内容是n!中末尾0的个数。
Sample Input
3 3 100 1024
Sample Output
0 24 253
Hint
这一题中的0的个数,将n个数素因子分解,0的个数就是2与5的对数,因为2会比5多的多,所以5的个数就是0的个数
#include <cstdio>
#include <cmath>
int main()
{
int T,n;
scanf("%d",&T);
for(int cas=1;cas<=T;cas++){
scanf("%d",&n);
int ans=0,p=5;
while(n>=p){
ans+=n/p;
p*=5;
}
printf("%d\n",ans);
}
return 0;
}