春風(fēng)博客

          春天里,百花香...

          導(dǎo)航

          <2008年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          統(tǒng)計

          公告

          MAIL: junglesong@gmail.com
          MSN: junglesong_5@hotmail.com

          Locations of visitors to this page

          常用鏈接

          留言簿(11)

          隨筆分類(224)

          隨筆檔案(126)

          個人軟件下載

          我的其它博客

          我的鄰居們

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          求階乘數(shù)中尾后零的個數(shù)

          主要是利用了10=2*5,一個數(shù)中5的因子個數(shù)必定比2的因子個數(shù)少的原理。

          代碼如下:

          package com.sitinspring;

          /**
           * 求階乘數(shù)中尾后零的個數(shù)
           * 如10!的尾后零的個數(shù)為2
           * 
          @author sitinspring(junglesong@gmail.com)
           * 
          @since 2008-6-10 上午11:57:45
           * @vsersion 1.00 創(chuàng)建 sitinspring 2008-6-10 上午11:57:45
           
          */

          public class PowerZeroCount{
              
          public static void main(String[] args){
                  
          int sum=0;
                  
          for(int i=1;i<=100;i++){
                      
          int count=getDivisorCount(i,5);
                      sum
          +=count;
                  }

                  
                  System.out.println(
          "100階乘尾后0的個數(shù)為:"+sum);
              }

              
              
          /**
               * 取得除數(shù)在被除數(shù)中的可整除次數(shù),如5在25中的可整除個數(shù)為2
               * 
          @param Dividend 被除數(shù)
               * 
          @param divisor 除數(shù)
               * 
          @return
               
          */

              
          public static int getDivisorCount(int Dividend,int divisor){
                  
          if(Dividend==0){
                      
          return 0;
                  }

                  
                  
          int count=0;
                  
                  
          if(Dividend % divisor==0){
                      count
          =1;
                      count
          +=getDivisorCount(Dividend/divisor,divisor);
                  }

                  
                  
          return count;
              }

              
              
          /**
               * 求某數(shù)階乘,但很快會溢出,擴大int為long也很快會溢出,放在這里只是做個演示
               * 
          @param n
               * 
          @return
               
          */

              
          public static int power(int n){
                  
          if(n<=1){
                      
          return 1;
                  }

                  
          else{
                      
          return n*power(n-1);
                  }

              }

          }

          輸出:
          100階乘尾后0的個數(shù)為:24

          posted on 2008-06-10 12:14 sitinspring 閱讀(643) 評論(0)  編輯  收藏 所屬分類: 算法數(shù)據(jù)結(jié)構(gòu)

          sitinspring(http://www.aygfsteel.com)原創(chuàng),轉(zhuǎn)載請注明出處.
          主站蜘蛛池模板: 赫章县| 莫力| 镇康县| 宁陵县| 霍山县| 阜新市| 绍兴县| 文登市| 鄂尔多斯市| 彰化县| 遂溪县| 芷江| 铁力市| 上饶县| 金堂县| 石家庄市| 桂东县| 布拖县| 周至县| 天柱县| 乌恰县| 喀喇| 二连浩特市| 九江县| 团风县| 灵璧县| 佛教| 攀枝花市| 定结县| 吉首市| 依安县| 沙湾县| 景德镇市| 武清区| 临桂县| 关岭| 瑞安市| 施甸县| 容城县| 文成县| 开封县|