zoukankan      html  css  js  c++  java
  • 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;
    }

  • 相关阅读:
    Oracle 分析函数
    Oracle 增加修改删除字段
    Oracle 重置序列
    End2EndIT
    Hyperledger Fabric SDK use case 1
    云计算中8项核心技术
    Cloud
    JVM Guide
    微信公众平台PHP开发
    在Linux系统环境下修改MySQL的root密码
  • 原文地址:https://www.cnblogs.com/emituofo/p/2761087.html
Copyright © 2011-2022 走看看