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

          常用鏈接

          留言簿(2)

          隨筆檔案

          文章分類

          文章檔案

          相冊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

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

          從小的數字看起..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嗯.是麻煩.所以要把公式提煉出來把這個算術式分析一下。就可以得到公式
          這個東西算出來就是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之間含有數字7的個數總和?  簡化為 求 10000以內的含有數字7的數的應該這樣9的3次方*10的0次方 + 9的2次方*10的1次方 + 9的1次方*10的2次方  +  9的0次方+10的3次方
          其實就是要求N位數的含有某數字的個數先求出N為數的最小數a然后計算a以內有多少個acount=9的n-2次方*10的0次方 + .... + 9的0次方*10的n-2次方.
          然后用該整數除以a得到該整數是a的b倍..再用b*acount.得出該整數減去該整數除以a的余數包含的所有包含該數字的數的數量依次類推..

          這個可以一直做下去。 2008080808 = 2000000000  先求出 2000000000 包含的個數  8000000  再求8000000內包含的個數      800   ...        8  ...

          (改天附上程序實現)


          一下是程序實現:
          package org.phyeas.demo;

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


              
          /**
               * 查找從從1到end含有數字hasNumber的個數
               * 
               * 
          @param end
               *            結束
               * 
          @param hasNumber
               *            含有某數字
               * 
          @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;
              }


              
          /**
               * 獲取與這個數字位數相同的最小數
               * 
               * 
          @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;
              }

          }

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

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

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


          網站導航:
           
          主站蜘蛛池模板: 内乡县| 临江市| 姚安县| 定边县| 文昌市| 山西省| 南澳县| 西吉县| 正镶白旗| 定边县| 昌邑市| 黑水县| 金堂县| 剑川县| 察隅县| 华亭县| 特克斯县| 阿拉善左旗| 新闻| 黔南| 张掖市| 察雅县| 安宁市| 莆田市| 万荣县| 三台县| 苏尼特左旗| 探索| 正阳县| 治多县| 喀喇沁旗| 会宁县| 宁阳县| 仁布县| 扎鲁特旗| 饶河县| 盐源县| 游戏| 衡水市| 义乌市| 长乐市|