笨鳥

          天道酬勤,思者常新;博觀約取,厚積薄發;心如止水,氣貫長虹;淡泊明志,寧靜致遠。
          posts - 10, comments - 0, trackbacks - 0, articles - 1

          2010年11月24日

          由于javaeye現在可以訪問,下面的內容將在javaeye中,博客地址http://clumsybird.javaeye.com

          posted @ 2010-11-24 17:56 ClumsyBird 閱讀(189) | 評論 (0)編輯 收藏

               摘要: 問題描述如下:     “一個20*20的數組如下所示          08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08      ...  閱讀全文

          posted @ 2010-11-24 16:03 ClumsyBird 閱讀(251) | 評論 (0)編輯 收藏

          2010年11月23日

              問題描述如下:
                  “10以下的質數之和為2+3+5+7=17,求2000000以下的質數之和?”

              此問題相對比較簡單,在前面的問題中已經給出質數判斷的方法,具體代碼如下:
          /**
               * 判斷是否是素數
               * 
               * 
          @param n
               * 
          @return
               
          */

              
          public static boolean isPrimeNumber(int n) {
                  
          if (n < 2{
                      
          return false;
                  }

                  
          double max = Math.sqrt(n);
                  
          for (int i = 2; i <= max; i++{
                      
          if (n % i == 0{
                          
          return false;

                      }

                  }

                  
          return true;
              }

              問題的實現方法如下:
          /**
               * 小于n的質數之和
               * 
          @param n
               * 
          @return
               
          */

              
          private static Long getPrimeNumberSum(int n) {
                  
          int i = 2;
                  Long sum 
          = 2L;
                  
          while (i <= n) {
                      
          if (AlgorithmUtil.isPrimeNumber(++i)) {
                          sum 
          += i;
                      }

                  }

                  
          return sum;
              }

              即可得到答案142913828922

              稍稍優化一下,
              /**
               * 小于n的質數之和
               * 
               * 
          @param n
               * 
          @return
               
          */

              
          private static Long getPrimeNumberSum(int n) {
                  
          int i = 5;
                  Long sum 
          = 5L;//由于2,3都是質數,初始值為5
                  while (i <= n) {
                      
          if (AlgorithmUtil.isPrimeNumber(i += 2)) {//質數 不能被2整除
                          sum += i;
                      }

                      
          if (i <= n && AlgorithmUtil.isPrimeNumber(i += 4)) {//不能被3整除
                          sum += i;
                      }

                  }

                  
          return sum;
              }
              還可以通過埃拉托斯特尼篩法(http://zh.wikipedia.org/zh-cn/%E5%9F%83%E6%8B%89%E6%89%98%E6%96%AF%E7%89%B9%E5%B0%BC%E7%AD%9B%E6%B3%95)來質數之和。

              請不吝賜教。
              @anthor ClumsyBird

          posted @ 2010-11-23 17:22 ClumsyBird 閱讀(397) | 評論 (0)編輯 收藏

              問題描述如下:
                  “畢達哥拉斯三元數組存在{a,b,c},a<b<c,使得a^2+b^2=c^2,如3^2+4^2=5^2=25,求a,b,c滿足以上條件,并使a+b+c=1000,給出a*b*c的值。”

              代碼如下:
          /**
               * 求畢達哥拉斯三元數組{a,b,c},使得a+b+c=target . 畢達哥拉斯三元數組存在{a,b,c},a<b<c,使得a^2+b^2=c^2
               * a > 3,(target-(a+b))^2=c^2=a^2+b^2 --> target^2=2*target*(a+b)-2ab
               * 
               * 
          @return
               
          */

              
          private static int getNumber(int target) {
                  
          int a = 0;
                  
          int b = 0;
                  
          int c = 0;
                  
          for (int i = 3; i < target; i++{
                      
          for (int j = i; j < target; j++{
                          
          if (target * target == 2 * target * i + 2 * target * j - 2 * i
                                  
          * j) {// target^2=2*target*(a+b)-2ab
                              a = i;
                              b 
          = j;
                              
          break;
                          }

                      }

                  }

                  c 
          = target - a - b;
                  
          return a * b * c;
              }

              具體的分析可以看代碼注釋。得出結果31875000。
              除了直接的辦法,應該還有另外的方法來求,保持未完待續狀態。
              請不吝賜教。
              @anthor ClumsyBird

          posted @ 2010-11-23 17:09 ClumsyBird 閱讀(348) | 評論 (0)編輯 收藏

              問題描述如下:
                  “對于1000位數值求出連續五位數值的最大乘積,1000位連續數值如下:
                              73167176531330624919225119674426574742355349194934
                  96983520312774506326239578318016984801869478851843
                  85861560789112949495459501737958331952853208805511
                  12540698747158523863050715693290963295227443043557
                  66896648950445244523161731856403098711121722383113
                  62229893423380308135336276614282806444486645238749
                  30358907296290491560440772390713810515859307960866
                  70172427121883998797908792274921901699720888093776
                  65727333001053367881220235421809751254540594752243
                  52584907711670556013604839586446706324415722155397
                  53697817977846174064955149290862569321978468622482
                  83972241375657056057490261407972968652414535100474
                  82166370484403199890008895243450658541227588666881
                  16427171479924442928230863465674813919123162824586
                  17866458359124566529476545682848912883142607690042
                  24219022671055626321111109370544217506941658960408
                  07198403850962455444362981230987879927244284909188
                  84580156166097919133875499200524063689912560717606
                  05886116467109405077541002256983155200055935729725
                  71636269561882670428252483600823257530420752963450

                          
                          如:紅色表示的連續5位數57831,其乘積為5*7*8*3*1”
              代碼實現如下:

          private static int getGreatestProductBy(String s) {
                  
          int max = 0;
                  
          for (int i = 0; i < s.length() - 5; i++{
                      
          int temp = Integer.parseInt(s.charAt(i) + "")
                              
          * Integer.parseInt(s.charAt(i + 1+ "")
                              
          * Integer.parseInt(s.charAt(i + 2+ "")
                              
          * Integer.parseInt(s.charAt(i + 3+ "")
                              
          * Integer.parseInt(s.charAt(i + 4+ "");
                      
          if (temp > max) {
                          max 
          = temp;
                      }

                  }

                  
          return max;
              }

          得答案40824

              請不吝賜教。
              @anthor ClumsyBird

          posted @ 2010-11-23 15:21 ClumsyBird 閱讀(255) | 評論 (0)編輯 收藏

              問題描述如下:
                  “前6個質數為:2,3,5,7,11,13,那第6個質數為13,求第10001個質數。”
              代碼如下:
          private static int getPrimeNumberBy(int n) {
                  
          int j = 1;
                  
          int i = 1;
                  
          int result = 0;
                  
          while (j < n) {
                      
          if (AlgorithmUtil.isPrimeNumber(i)) {
                          result 
          = i;
                          j
          ++;
                      }

                      i 
          += 2;
                  }

                  
          return result;
              }
              下面是判斷質數的代碼:
          /**
               * 判斷是否是素數
               * 
               * 
          @param n
               * 
          @return
               
          */

              
          public static boolean isPrimeNumber(int n) {
                  
          if (n < 2{
                      
          return false;
                  }

                  
          double max = Math.sqrt(n);
                  
          for (int i = 2; i <= max; i++{
                      
          if (n % i == 0{
                          
          return false;

                      }

                  }

                  
          return true;
              }
              ps:質數也叫素數。

              請不吝賜教。
              @anthor ClumsyBird

          posted @ 2010-11-23 13:56 ClumsyBird 閱讀(875) | 評論 (0)編輯 收藏

              問題描述如下:
                  “1到10的平方和為:1^2 + 2^2 + ... + 10^2 = 385,和平方為:(1 + 2 + ... + 10)^2 = 55^2 = 3025,他們之間的差為3025-385=2640,求1到100的和平方與平方和之間的差值?”

              代碼實現如下:

              /**
               * 求前n個自然數和平方與平方和之差
               * 
          @param n
               * 
          @return
               
          */

              
          private static int getDifference(int n) {
                  
          int first = 0;
                  
          int second = 0;
                  
          for (int i = 1; i <= n; i++{
                      first 
          += i * i;
                      second 
          += i;
                  }

                  
          return second * second - first;
              }
              可以得到答案25164150。

              我們還可以使用數學的方法來解此題。
              1^2 + 2^2 + ... + n^2 =n(n+1)(2n+1)/6
              (1+2+3+...+n)^2 =(n(n+1)/2)^2
              相關證明可以去具體的了解,如:
              (n+1)^3 -(n-1)^3 =6n^2+2提示:證明平方和公式,1到n的求和公式就不提示了
              給出代碼:

              private static int getDifference1(int n) {
                  
          return (n*(n+1)/2)*(n*(n+1)/2)-n*(n+1)*(2*n+1)/6;
              }

              到此結束,請不吝賜教。
              @anthor ClumsyBird

          posted @ 2010-11-23 13:37 ClumsyBird 閱讀(567) | 評論 (0)編輯 收藏

              問題敘述如下:
              “2520是最小的數能夠整除1到10,求能被1到20所整除的最小的數?”
            代碼如下:

          /**
               * 數字i從m到n,遍歷,如果i不能被result整除,我們就將i除以i與result的最大公約數,并與當前result想乘
               *
               
          */

              
          private static int getNumber(int m, int n) {
                  
          int result =
           n;
                  
          for (int i = n - 1; i >= m; i--
          {
                      
          if (result % i != 0
          {
                          result 
          *= i/
          gcd(result,i);
                      }

                  }

                  
          return result;
              }


              
          /**
               * 最大公約數:歐幾里德算法 定理:gcd(a,b) = gcd(b,a mod b)
               * 
               * 
          @param a
               * 
          @param
           b
               * 
          @return

               
          */

              
          private static int gcd(int a, int b) {
                  
          if (b != 0
          )
                      
          return gcd(b, a %
           b);
                  
          else

                      
          return a;
              }

              調用getNumber(1,20)即可得到答案232792560
              由于在此用到最大公約數,所以在下面給出了一些實現。

              /**
               * 最大公約數:歐幾里德算法
               * 
          @param a
               * 
          @param
           b
               * 
          @return

               
          */

              
          private static int gcd1(int a, int b) {
                  
          if (a > b) 
          {
                      gcd1(b, a);
                  }

                  
          int temp = 0;
                  
          for (; b != 0;) 
          {
                      temp 
          = a %
           b;
                      a 
          =
           b;
                      b 
          =
           temp;
                  }

                  
          return a;
              }


              
          /**
               * 最大公約數:Stein算法 gcd(ka,kb) = k gcd(a,b),也就是最大公約數運算和倍乘運算可以交換,
               * 特殊的,當k=2時,說明兩個偶數的最大公約數必然能被2整除
               * 
               * 
          @param a
               * 
          @param
           b
               * 
          @return

               
          */

              
          private static int gcdByStein(int a, int b) {
                  
          if (a > b) 
          {
                      gcdByStein(b, a);
                  }

                  
          if (b == 0{
                      
          return
           a;
                  }

                  
          if (a % 2 == 0 && b % 2 == 0{
                      
          return 2 * gcdByStein(a / 2, b / 2);//a,b都是偶數

                  }

                  
          if (a % 2 == 0{
                      
          return gcdByStein(a / 2, b);//僅a為偶數

                  }

                  
          if (b % 2 == 0{
                      
          return gcdByStein(a, b / 2);//僅b為偶數

                  }

                  
          return gcdByStein((a + b) / 2, (a - b) / 2);//a,b都是奇數
              }

              如果有其他的方法,也請貼出大家一起討論。^_^
              請不吝賜教。
              @anthor ClumsyBird

          posted @ 2010-11-23 13:14 ClumsyBird 閱讀(276) | 評論 (0)編輯 收藏

          2010年11月11日

              Elcipse編輯功能比較強大,掌握eclipse快捷鍵,會很好的提高開發效率,下面就是一些常用快捷鍵介紹。
              1. [ALT+/] 
                  能為用戶提供內容的輔助,不要為記不全方法和屬性名稱犯愁,當記不全類、方法和屬性的名字時,可以使用它。
              2. [Ctrl+O]
                  顯示類中方法和屬性的大綱,能快速定位類的方法和屬性。
              3. [Ctrl+/][Ctrl+Shift+/][Ctrl+Shift+\]
                  添加或取消注釋//,加上段注釋/**/,取消段注釋/**/
              4. [Ctrl+D][Ctrl+Delete/Backspace][Ctrl+Shift+Delete]
                  刪除當前行,刪除下一個/上一個單詞,刪除到行末
              5. [Ctrl+M]
                  窗口最大化和還原。
              6. [Ctrl+K][Ctrl++Shift+K]
                  快速向下和向上查找選定的內容。
              7. [Ctrl+Shift+T]
                  查找Workspace構建路徑中可找到的Java類文件,而且可以使用“*”等通配符。
              8. [Ctrl+Shift+R]
                  和[Ctrl+Shift+T]對應,查找Workspace中的所有文件(包括Java文件),也可以使用通配符。
              9. [Ctrl+Shift+G][Ctrl+Alt+G][Ctrl+G]
                  搜索選中元素的引用,搜索選中的文本,搜索選中元素的聲明
              10. [Ctrl+Shift+O][Ctrl+Shift+M]
                  快速生成import,添加選中導入
              11. [Ctrl+Shift+F]
                  格式化代碼。
              12. [ALT+Shift+W]
                  快速定位瀏覽器視圖的位置。
              13. [Ctrl+L]
                  定位到某一行。
              14. [Alt+←][Alt+]
                  后退前進歷史記錄。
              15. [F3][Ctr+鼠標左鍵]
                  快速定位光標處的某個方法,屬性或者類。
              16. [F4]
                  顯示類的繼承關系,并打開類繼承圖。
              17. [Ctrl+Shift+B]
                  設置斷點或者取消斷點。
              18. [F11]
                  調試最后一次運行的程序。
              19. [Ctrl+F11]
                  運行最后一次運行的程序。
              20. [F5]
                  debug時跟蹤到某一個方法中。
              21. [F6]
                  單步debug。
              22. [F7]
                  debug時[F7]會執行完方法,返回到調用此方法的后一條語句。
              23. [F8]
                  debug時,如果使用[F8]就繼續執行,到下一個斷點或程序結束。
              24. [Ctrl+C][Ctrl+V][Ctrl+X][Ctrl+Z][Ctrl+S][Ctrl+F][Ctrl+Y
                  復制,粘貼,剪切,撤消,保存,查找,重復。
              25. [Ctrl+F6][Ctrl+Shift+F6][Ctrl+F7][Ctrl+Shift+F7][Ctrl+F8][Ctrl+Shift+F8]
                  切換到下一個編輯器,上一個編輯器,下一個視圖,上一個視圖,下一個透視圖,上一個透視圖。
              26. [Ctrl+1]
                  快速修正,很有用。
              27. [Shift+F2]
                  打開外部java文檔。
              28. [Ctrl+H]
                  打開搜索對話框。
              29. [Alt+Shift+T][Alt+Shift+C][Alt+Shift+V][Alt+Shift+R]
                  快速顯示重構菜單,重構-改變方法簽名,重構-移動,重構-重命名
              30. [Alt+Left/Right][Ctrl+Shift+Left/Right/Up/Down][Alt+Shift+Up/Down/Left/Right][Alt+Up/Down][Ctrl+Alt+Up/Down]
                  上一次/下一次光標所在位置,上一個單詞/下一個單詞/上一個成員/下一個成員(成員對象或成員函數),選中閉合元素,上下移動選中的行,拷貝選中的行。
              31. [Ctrl+Shift+Enter][Shift+Enter]
                  在當前行上插入一行,在當前行下插入一行。
              32. [End/Home][Ctrl+Right/Left][Ctrl+Up/Down][Shift+End/Home]
                  行末/行首,前一個/后一個單詞,上下滾屏,選中到行末/行首
              33. [Ctrl+T][Ctrl+Alt+H]
                  快速層次結構,打開調用層次結構。
              34. [Ctrl+Q]
                  上一個編輯的位置。
              35. [Ctrl+Shift+S]
                  保存所有。
              36. [Ctrl+.]
                  下一個命中的項(搜索之后)。
              37. [Ctrl+Shift+X/Y]
                  變為大/小寫
                      
            大家有好用的快捷鍵,可以一起分享。 @anthor ClumsyBird 

          posted @ 2010-11-11 00:11 ClumsyBird 閱讀(193) | 評論 (0)編輯 收藏

          一 安裝git
              http://code.google.com/p/msysgit/downloads/list下載,安裝即可。

          android代碼倉庫
              http://android.git.kernel.org 可以看到android所有git包的列表。

          三 下載android source
              在windows下創建目錄android src,右擊選擇Git Bush,輸入git clone git://android.git.kernel.org/platform/external/v8.git,即可下載external/v8下的源代碼.其他的源碼下載以此類推。

          四 命令解釋
              git clone是命令git://android.git.kernel.org/platform/external/v8.git,可以選擇git包列表的一項,點擊進入,在http://android.git.kernel.org 中可以看到。

          @anthor ClumsyBird

          posted @ 2010-11-11 00:03 ClumsyBird 閱讀(795) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 宁武县| 涟水县| 安阳市| 连云港市| 克什克腾旗| 新丰县| 长岭县| 安宁市| 科尔| 锡林郭勒盟| 巴塘县| 从江县| 吉首市| 大姚县| 洛扎县| 溆浦县| 嵊泗县| 舟山市| 阳曲县| 扎囊县| 南江县| 威海市| 同江市| 阜康市| 拉萨市| 闸北区| 彝良县| 双辽市| 合江县| 巴里| 来凤县| 五常市| 湛江市| 城固县| 南平市| 胶南市| 大渡口区| 壶关县| 依安县| 红桥区| 乐东|