算法题:阶乘尾零

题目描写叙述

请设计一个算法。计算n的阶乘有多少个跟随零。

给定一个int n。请返回n的阶乘的尾零个数。保证n为正整数。
測试例子:

5

返回:1

#include <iostream>
using namespace std;
int Grial(int x)
{
    int temp = x;
    int count2 = 0;
    int count5 = 0;
    while (temp)
    {
        count2 += temp / 2;
        temp /= 2;
        //这里有一个重点,如求n!中2的个数,count=n/2+n/(2*2)....。
        //比方求n!中3的个数。

/* int count = 0; while(n) { count+=n/3; n/=3;//就是这么简单。 } */ } temp = x; while (temp) { count5 += temp / 5; temp /= 5; } //求n!中0的个数无非就是求2。5的个数之比中。个数最小的那个的个数。 return count2 > count5 ? count5 : count2; } int main() { cout<<Grial(1000)<<endl; }

原文地址:https://www.cnblogs.com/yutingliuyl/p/7060571.html