隨筆 - 25  文章 - 32  trackbacks - 0
          <2008年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(2)

          隨筆檔案

          文章分類

          文章檔案

          相冊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          在CSDN上看到的題..看到很多人的算法都挺復(fù)雜的..

          從小的數(shù)字看起..10里有1個100是10的10倍.但是100里還有70-79都是..所以就是 1*9+10=19個
          那么1000就是  (1*9+10)*9+100
          10000就是  ((1*9+10)*9+100)*9+1000嗯.是麻煩.所以要把公式提煉出來把這個算術(shù)式分析一下。就可以得到公式
          這個東西算出來就是9*9*9+810+900+1000=9的三次方*10的0次方+9的2次方*10的一次方 + 9的1次方*10的2次方+9的0次方*10的3次方
                                                                            (9*9*9)              (810)                                (900)                           (1000)

          如果把  求1到2008080808之間含有數(shù)字7的個數(shù)總和?  簡化為 求 10000以內(nèi)的含有數(shù)字7的數(shù)的應(yīng)該這樣9的3次方*10的0次方 + 9的2次方*10的1次方 + 9的1次方*10的2次方  +  9的0次方+10的3次方
          其實就是要求N位數(shù)的含有某數(shù)字的個數(shù)先求出N為數(shù)的最小數(shù)a然后計算a以內(nèi)有多少個acount=9的n-2次方*10的0次方 + .... + 9的0次方*10的n-2次方.
          然后用該整數(shù)除以a得到該整數(shù)是a的b倍..再用b*acount.得出該整數(shù)減去該整數(shù)除以a的余數(shù)包含的所有包含該數(shù)字的數(shù)的數(shù)量依次類推..

          這個可以一直做下去。 2008080808 = 2000000000  先求出 2000000000 包含的個數(shù)  8000000  再求8000000內(nèi)包含的個數(shù)      800   ...        8  ...

          (改天附上程序?qū)崿F(xiàn))


          一下是程序?qū)崿F(xiàn):
          package org.phyeas.demo;

          public class Main {
              
          public static void main(String[] args) {
                  System.out.println(lookUpForHasNumber(
          2008080808 , 7));
              }


              
          /**
               * 查找從從1到end含有數(shù)字hasNumber的個數(shù)
               * 
               * 
          @param end
               *            結(jié)束
               * 
          @param hasNumber
               *            含有某數(shù)字
               * 
          @return
               
          */

              
          public static int lookUpForHasNumber(long end, int hasNumber) {
                  
          int count = 0;
                  
          long less = getLess(end);
                  
          while (less > 10{
                      
          int n = (int) Math.log10(less);
                      
          long bei = end / less;
                      
          int tempCount = 0;
                      
          for (int i = n - 1, j = 0; i >= 0; i--, j++{
                          tempCount 
          += Math.pow(9, i) * Math.pow(10, j);
                      }

                      
          if (bei >= 1{
                          
          if (bei > hasNumber) {
                              tempCount 
          = (int) ((int) (tempCount * (bei - 1)) + less);
                          }
           else if (bei == hasNumber) {
                              tempCount 
          = (int) (tempCount * bei) + 1;
                          }
           else {
                              tempCount 
          = (int) (tempCount * bei);
                          }

                      }

                      count 
          += tempCount;
                      
                      end 
          = end - less * bei;
                      less 
          = getLess(end);
                  }

                  
          if (less > hasNumber) {
                      count 
          = count + 1;
                  }

          //        System.out.println("count=" + count);
                  return count;
              }


              
          /**
               * 獲取與這個數(shù)字位數(shù)相同的最小數(shù)
               * 
               * 
          @param number
               * 
          @return
               
          */

              
          public static long getLess(long number) {
                  
          if (number > 10{
                      String str 
          = String.valueOf(number);
                      StringBuffer nStr 
          = new StringBuffer("1");
                      
          for (int i = 1; i < str.length(); i++{
                          nStr.append(
          "0");
                      }

                      
          return Long.parseLong(nStr.toString());
                  }

                  
          return number;
              }

          }

          最后結(jié)果:1229473242
          posted on 2008-06-18 23:11 phyeas 閱讀(386) 評論(1)  編輯  收藏

          FeedBack:
          # re: 求1到2008080808之間含有數(shù)字7的個數(shù)總和? 其實沒那么復(fù)雜 2008-06-20 09:37 phyeas
          上面的公式解釋可能有誤..其實應(yīng)該是加入該數(shù)為10的n次方.那么求該數(shù)內(nèi)含有某數(shù)字(1-9)的數(shù)的總和為
          count=9的n-1次方*10的0次方 + .... + 9的0次方*10的n-1次方  回復(fù)  更多評論
            

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 界首市| 怀柔区| 宁乡县| 霍邱县| 肥乡县| 贵港市| 永德县| 宝坻区| 潍坊市| 阿图什市| 湘乡市| 柳州市| 南川市| 衡阳县| 陆川县| 宣城市| 石渠县| 临湘市| 绥中县| 赤城县| 深水埗区| 大城县| 东莞市| 湖南省| 海伦市| 容城县| 虞城县| 鄂托克旗| 新营市| 竹山县| 太原市| 清流县| 井冈山市| 固原市| 缙云县| 修文县| 女性| 来安县| 绍兴市| 漳平市| 大新县|