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

          常用鏈接

          留言簿(2)

          隨筆檔案

          文章分類

          文章檔案

          相冊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

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

          從小的數(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嗯.是麻煩.所以要把公式提煉出來把這個算術式分析一下。就可以得到公式
          這個東西算出來就是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ù)的應該這樣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
               *            結束
               * 
          @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;
              }

          }

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

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

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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 库尔勒市| 石屏县| 黄骅市| 松潘县| 灌阳县| 石城县| 宜良县| 溧水县| 九江县| 汝南县| 宜章县| 陕西省| 孟村| 游戏| 大港区| 宕昌县| 华阴市| 新乡县| 罗田县| 逊克县| 彭水| 冕宁县| 安丘市| 河曲县| 曲靖市| 祁门县| 麦盖提县| 泗水县| 达尔| 康马县| 繁峙县| 江油市| 金湖县| 新建县| 罗平县| 云南省| 利津县| 桐柏县| 绵阳市| 岳阳县| 封丘县|