春風博客

          春天里,百花香...

          導航

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

          統計

          公告

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

          Locations of visitors to this page

          常用鏈接

          留言簿(11)

          隨筆分類(224)

          隨筆檔案(126)

          個人軟件下載

          我的其它博客

          我的鄰居們

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          求階乘數中尾后零的個數

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

          代碼如下:

          package com.sitinspring;

          /**
           * 求階乘數中尾后零的個數
           * 如10!的尾后零的個數為2
           * 
          @author sitinspring(junglesong@gmail.com)
           * 
          @since 2008-6-10 上午11:57:45
           * @vsersion 1.00 創建 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的個數為:"+sum);
              }

              
              
          /**
               * 取得除數在被除數中的可整除次數,如5在25中的可整除個數為2
               * 
          @param Dividend 被除數
               * 
          @param divisor 除數
               * 
          @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;
              }

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

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

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

              }

          }

          輸出:
          100階乘尾后0的個數為:24

          posted on 2008-06-10 12:14 sitinspring 閱讀(640) 評論(0)  編輯  收藏 所屬分類: 算法數據結構

          sitinspring(http://www.aygfsteel.com)原創,轉載請注明出處.
          主站蜘蛛池模板: 瓦房店市| 百色市| 镇远县| 曲麻莱县| 抚顺市| 城步| 武夷山市| 高碑店市| 民乐县| 舟山市| 师宗县| 贞丰县| 嘉义县| 张家川| 兴山县| 班戈县| 宜君县| 绿春县| 黑水县| 海南省| 邛崃市| 平江县| 德庆县| 汝阳县| 上虞市| 双鸭山市| 彰化市| 紫阳县| 临安市| 甘泉县| 汉寿县| 西宁市| 上饶市| 景洪市| 临漳县| 五莲县| 什邡市| 阿克苏市| 临海市| 潍坊市| 武穴市|