一个数 n 的阶乘末尾有多少个 0 取决于从 1 到 n 的各个数的因子中 2 和 5 的个数, 而 2 的个数是远远多余 5 的个数的, 因此求出 5 的个数即可. 题解中给出的求解因子 5 的个数的方法是用 n 不断除以 5, 直到结果为 0, 然后把中间得到的结果累加.
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
int t;
scanf(
"%d",&t);
while(t--)
{
int n;
scanf(
"%d",&n);
int res=
0;
while(n)
{
res+=n/
5;
n/=
5;
}
printf(
"%d\n",res);
}
return 0;
}