春風博客

          春天里,百花香...

          導航

          <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)原創,轉載請注明出處.
          主站蜘蛛池模板: 唐海县| 喜德县| 芜湖县| 潜山县| 静安区| 贵州省| 祁阳县| 九龙县| 祁连县| 登封市| 汶上县| 磐安县| 江阴市| 长治县| 双江| 玛沁县| 张家界市| 抚远县| 肃北| 潮安县| 龙南县| 海晏县| 昌邑市| 南涧| 宝丰县| 阿合奇县| 惠安县| 彭山县| 天气| 吉安县| 洛阳市| 绥宁县| 东乡族自治县| 封开县| 伊宁县| 清水县| 吴桥县| 南平市| 林甸县| 湖州市| 平定县|