笨鳥

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

          2010年11月23日

          由于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)編輯 收藏

              問題描述如下:
                  “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 閱讀(349) | 評論 (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 閱讀(277) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 望江县| 万安县| 观塘区| 江都市| 清新县| 泰宁县| 万山特区| 津市市| 宁武县| 禹州市| 藁城市| 长垣县| 同德县| 林西县| 崇义县| 工布江达县| 明水县| 连城县| 上杭县| 浮山县| 达日县| 犍为县| 万宁市| 南雄市| 莒南县| 芮城县| 思茅市| 寿宁县| 扎鲁特旗| 桓台县| 台东市| 平邑县| 堆龙德庆县| 盐边县| 集安市| 咸宁市| 两当县| 锦州市| 饶平县| 岳池县| 合阳县|