51nod 1003 阶乘后面0的数量

基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题
 收藏
 关注
n的阶乘后面有多少个0?
6的阶乘 = 1*2*3*4*5*6 = 720,720后面有1个0。
Input
一个数N(1 <= N <= 10^9)
Output
输出0的数量
Input示例
5
Output示例
1

题意:中文

思路:末尾零的个数是由2和5组成的,2的数量明显很多,计算5的数量就是答案

有效求因子5的个数的方法是:n/(5^i),然后求和

#include <iostream>
#include<math.h>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
int main()
{
    int fivePow[]={1,5,25,125,625,3125,15625,78125,390625,1953125,9765625,48828125,244140625};
    int n;
    while(cin>>n)
    {
        int sum=0;
        for(int i=1;i<13;i++)
        {
            sum+=n/fivePow[i];
        }
        cout<<sum<<endl;
    }
    return 0;
}






原文地址:https://www.cnblogs.com/bryce1010/p/9387322.html