C/C++计算阶乘n!末尾所含0的个数

#include <iostream>
using namespace std;

int SumFactZeros(const int n)
{
    if(n<0) exit(0);
    if(n<5) return 0;
    int counter=0;  
    for(int i=5;i<=n;i++){
       int flag=i;
       while(flag%5==0)
       {
            flag/=5;
         counter++;
       }
    }
    return counter;
}

int SumFactZeros_recursion(const int n)
{
    if(n<0) exit(0);
    if(n<5) return 0;
    int k=n/5;
    return k+SumFactZeros_recursion(k);
}

int main()
{
    for(int n=5;n<=1000;n++)
       if(SumFactZeros(n)==SumFactZeros_recursion(n))
           cout<<n<<"! has "<<SumFactZeros(n)<<" zeros at the end."<<endl;
       else
           cout<<n<<"! has (sorry, difference answers) zeros at the end."<<endl;
   
    system("pause");
    return 0;
}

原文链接: https://www.cnblogs.com/emituofo/archive/2012/11/08/2761087.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    C/C++计算阶乘n!末尾所含0的个数

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/68590

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 下午1:24
下一篇 2023年2月9日 下午1:24

相关推荐