zoukankan      html  css  js  c++  java
  • n!末尾有多少个0以及n!末尾第一个非0数字

    (1)n!末尾有多少个0

        第一种思路:

        n!=n*(n-1)*(n-2)*....3*2*1,而如果要出现0,必须得有2和5出现,但是明显n!中5的因子个数少于2的因子个数,即转化为求 n!中因子5的个数

        int count(int n)

        {  

        int sum=0;

               while(n)

               {

          sum+=n/5;

                     n/=5;

               }

               return sum;

         }

         第二种思路:计算n!,每次末尾出现0,则将0消去,计数器并自增1

          int count=0;

          int ans=1;

            for(i=n;i>=2;i--)
            {
                ans*=i;
                while(ans%10==0)        //消除末尾的0
                {

                    ans/=10;

                    count++;
                }
                if(ans>=100000)           //只需保存末尾5位数字即可
                    ans%=100000;

            }

              while(ans%10==0)        //消除末尾的0
              {

                    ans/=10;

                    count++;
              }

             count即为所求

    (2)n!末尾第一个非0数字

        两种思路

        第一种:直接求出n!,然后得出结果

        第二种:按照问题(1)中的第二种思路解决即可。

  • 相关阅读:
    Intellij Idea 2017创建web项目及tomcat部署实战
    使用docker安装mysql服务
    python2--升级python3
    SpringCloud--注册中心Eureka
    SpringBoot--属性加载顺序
    Jmeter--压测dubbo接口
    较快的maven的settings.xml文件
    Spring boot:logback文件配置
    Spring--AOP
    34组代码敲不队记账类app会议纪要
  • 原文地址:https://www.cnblogs.com/dolphin0520/p/2014529.html
Copyright © 2011-2022 走看看