计算N!后面有多少个0.
主要思路就是从1到N个数中有多少个因子5
#include <iostream>
using namespace std;
int main()
{
int n;
cin >> n;
int factor5_count = 0;
// 查找第一个能被5整除的数字,也就是含有5因子的数字。
while (n % 5 != 0)
{
n --;
}
while (n > 0)
{
// 对每一个含有5因子的数字,进一步计算含有几个5因子
int tmp = n;
while (tmp % 5 == 0)
{
factor5_count ++;
tmp /= 5;
}
// 跳到下一个5的倍数处理
n -= 5;
}
cout << factor5_count << endl;
return 0;
}