隨筆 - 312, 文章 - 14, 評論 - 1393, 引用 - 0
          數(shù)據(jù)加載中……

          一著名軟件公司的java筆試算法題的答案

          本文為原創(chuàng),如需轉(zhuǎn)載,請注明作者和出處,謝謝!

              原題如下:用1、2、2、3、4、5這六個(gè)數(shù)字,用java寫一個(gè)程序,打印出所有不同的排列,如:512234、412345等,要求:"4"不能在第三位,"3"與"5"不能相連。

           解題思路:

              很明顯,這是一個(gè)遞歸算法。我們可以排列將這6個(gè)數(shù)按從小到大的順序排一下,如果是1,2,3,4,5,6,那么會(huì)有1*2*3*4*5*6= 6!=720個(gè)遞增的數(shù)。但如果是1,2,2,3,4,5,那么在這720個(gè)數(shù)中一定會(huì)有相同的數(shù)對出現(xiàn)(由于在這6個(gè)數(shù)中只有兩個(gè)數(shù)兩同,也就是說,如果有重復(fù)的數(shù),那么一定是一對數(shù),如122345會(huì)出現(xiàn)兩次)。

              排列的基本規(guī)則是分步進(jìn)行。也就是說,要排列上面6個(gè)數(shù),首先應(yīng)該選擇第一個(gè)數(shù),這第一個(gè)數(shù)可以選擇這6個(gè)數(shù)中的任意一個(gè),如選擇1.第二步是選擇第二個(gè)數(shù),這第二個(gè)數(shù)不能再選擇已經(jīng)選過的數(shù),如1.因此,它只能從后面5個(gè)數(shù)中選擇。如選擇2。以此類推。

              我們也可以在程序中模擬這一過程。源程序如下:

          public class test1
          {
              
          private int[] numbers = new int[]
              { 
          123345 };
              
          public int n;
              
          private String lastResult = "";

              
          private boolean validate(String s)
              {
                  
          if (s.compareTo(lastResult) <= 0)
                      
          return false;
                  
          if (s.charAt(2== '4')
                      
          return false;
                  
          if (s.indexOf("35">= 0 || s.indexOf("53">= 0)
                      
          return false;
                  
          return true;
              }

              
          public void list(String index, String result)
              {
                  
          for (int i = 0; i < numbers.length; i++)
                  {
                      
          if (index.indexOf(i + 48< 0)
                      {
                          String s 
          = result + String.valueOf(numbers[i]);
                          
          if (s.length() == numbers.length)
                          {
                              
          if (validate(s))
                              {
                                  System.out.println(s);
                                  lastResult 
          = s;
                                  n
          ++;
                              }
                              
          break;
                          }
                          list(index 
          + String.valueOf(i), s);
                      }
                  }
              }

              
          public static void main(String[] args)
              {
                  test1 t 
          = new test1();
                  t.list(
          """");
                  System.out.println(
          "總數(shù):" + t.n);

              }
          }


              其中l(wèi)ist函數(shù)是這個(gè)算法的核心函數(shù)。index參數(shù)表示已經(jīng)選擇過的數(shù),用numbers數(shù)組的索引表示。如index="012",表示numbers的前三個(gè)數(shù)已經(jīng)被選擇,也表示應(yīng)該選擇第四個(gè)數(shù)了,而這第四個(gè)數(shù)應(yīng)該從后三個(gè)數(shù)中選擇。result參數(shù)表示臨時(shí)的數(shù)字組合(這個(gè)數(shù)字組合最多是5個(gè)數(shù)字,因?yàn)椋绻搅?個(gè)數(shù)字,就表示已經(jīng)有一個(gè)結(jié)果產(chǎn)生了)。在默認(rèn)情況下index和result的值都是""。

              在validate中使用了  if (s.compareTo(lastResult) <= 0)進(jìn)行判斷,由于按這種方法進(jìn)行排列,如果這6個(gè)數(shù)是遞增給出的,那么排列的結(jié)果一定是遞增的,但上述的6個(gè)數(shù)其中第2和第3個(gè)位置上都是2,因此,如果出現(xiàn)了上一個(gè)結(jié)果不小于當(dāng)前結(jié)果的情況,一定是有重復(fù)了,因此,要將這部分?jǐn)?shù)過濾出去。

          使用1, 2, 2, 3, 4, 5的測試結(jié)果
          122345
          122543
          123245
          123254
          123425
          123452
          125234
          125243
          125423
          125432
          132245
          132254
          132425
          132452
          132524
          132542
          142325
          142523
          143225
          143252
          145223
          145232
          152234
          152243
          152324
          152342
          152423
          152432
          212345
          212543
          213245
          213254
          213425
          213452
          215234
          215243
          215423
          215432
          221345
          221543
          223145
          223154
          223415
          223451
          225134
          225143
          225413
          225431
          231245
          231254
          231425
          231452
          231524
          231542
          232145
          232154
          232415
          232451
          232514
          232541
          241325
          241523
          242315
          242513
          243125
          243152
          243215
          243251
          245123
          245132
          245213
          245231
          251234
          251243
          251324
          251342
          251423
          251432
          252134
          252143
          252314
          252341
          252413
          252431
          312245
          312254
          312425
          312452
          312524
          312542
          315224
          315242
          315422
          321245
          321254
          321425
          321452
          321524
          321542
          322145
          322154
          322415
          322451
          322514
          322541
          325124
          325142
          325214
          325241
          325412
          325421
          341225
          341252
          341522
          342125
          342152
          342215
          342251
          342512
          342521
          345122
          345212
          345221
          412325
          412523
          413225
          413252
          415223
          415232
          421325
          421523
          422315
          422513
          423125
          423152
          423215
          423251
          425123
          425132
          425213
          425231
          431225
          431252
          431522
          432125
          432152
          432215
          432251
          432512
          432521
          451223
          451232
          451322
          452123
          452132
          452213
          452231
          452312
          452321
          512234
          512243
          512324
          512342
          512423
          512432
          513224
          513242
          513422
          521234
          521243
          521324
          521342
          521423
          521432
          522134
          522143
          522314
          522341
          522413
          522431
          523124
          523142
          523214
          523241
          523412
          523421
          541223
          541232
          541322
          542123
          542132
          542213
          542231
          542312
          542321
          543122
          543212
          543221
          總數(shù):198


          使用1,2, 3, 3, 4, 5的測試結(jié)果
          123345
          125433
          132345
          132543
          133245
          133254
          133425
          133452
          143325
          145233
          152334
          152343
          152433
          213345
          215433
          231345
          231543
          233145
          233154
          233415
          233451
          243315
          245133
          251334
          251343
          251433
          312345
          312543
          313245
          313254
          313425
          313452
          315234
          315243
          315423
          315432
          321345
          321543
          323145
          323154
          323415
          323451
          325134
          325143
          325413
          325431
          331245
          331254
          331425
          331452
          331524
          331542
          332145
          332154
          332415
          332451
          332514
          332541
          341325
          341523
          342315
          342513
          343125
          343152
          343215
          343251
          345123
          345132
          345213
          345231
          413325
          415233
          423315
          425133
          431325
          431523
          432315
          432513
          433125
          433152
          433215
          433251
          451233
          451323
          451332
          452133
          452313
          452331
          512334
          512343
          512433
          513234
          513243
          513324
          513342
          513423
          513432
          521334
          521343
          521433
          523134
          523143
          523314
          523341
          523413
          523431
          541233
          541323
          541332
          542133
          542313
          542331
          543123
          543132
          543213
          543231
          543312
          543321
          總數(shù):118

          使用1, 3, 3, 3, 4, 5的測試結(jié)果

          133345
          313345
          315433
          331345
          331543
          333145
          333154
          333415
          333451
          343315
          345133
          433315
          451333
          513334
          513343
          513433
          541333
          543133
          543313
          543331
          總數(shù):20




          Android開發(fā)完全講義(第2版)(本書版權(quán)已輸出到臺灣)

          http://product.dangdang.com/product.aspx?product_id=22741502



          Android高薪之路:Android程序員面試寶典 http://book.360buy.com/10970314.html


          新浪微博:http://t.sina.com.cn/androidguy   昵稱:李寧_Lining

          posted on 2008-05-10 09:19 銀河使者 閱讀(8077) 評論(11)  編輯  收藏 所屬分類: javaalgorithm 原創(chuàng)

          評論

          # re: 一著名軟件公司的java筆試算法題的答案  回復(fù)  更多評論   

          請問index.indexOf(i + 48) < 0判斷條件的作用是什么?
          2008-05-10 12:10 | fengsuiyijing

          # re: 一著名軟件公司的java筆試算法題的答案  回復(fù)  更多評論   

          index保存了當(dāng)前數(shù)字串的索引,index.indexOf(i + 48) < 0就是判斷當(dāng)前的數(shù)字的索引是否在index中(由于給定的數(shù)字串有重復(fù)的數(shù)字,因此,只能使用索引來判斷了),如果不在,就會(huì)認(rèn)為這個(gè)數(shù)字還沒有加到index中,如果存在了,說明這個(gè)數(shù)已經(jīng)在數(shù)字串中了。


          注:i+48 ,當(dāng)i=0時(shí),正好是數(shù)字0的ASCII,以此類推
          2008-05-10 12:41 | 銀河使者

          # re: 一著名軟件公司的java筆試算法題的答案  回復(fù)  更多評論   

          你給出的想法和代碼都很優(yōu)秀,我覺得你對本題有兩點(diǎn)非常獨(dú)特的見解:1)你會(huì)使用字符索引來“組合”最后的字符串,而且?guī)滋幱玫降膇ndexOf非常合理,也很巧妙;2)validate函數(shù)的使用,尤其是判斷重復(fù)字符串上面,很難想到。我建議你還能去網(wǎng)上找到兩個(gè)版本的程序,其中一個(gè)也是用遞歸,另一個(gè)是圖的思想,一定要做多組測試,因?yàn)榫W(wǎng)上好多的程序都是錯(cuò)誤的,你這幾組的數(shù)據(jù)都是對的。

          補(bǔ):在“注”中有筆誤,當(dāng)i=0的時(shí)候,i+48是0的ASCII,因?yàn)槟鉯ndex中存的畢竟是索引,是位置嘛,所以是從0開始的
          2008-05-16 10:32 | dreamingnest

          # re: 一著名軟件公司的java筆試算法題的答案  回復(fù)  更多評論   

          to dreamingnest

          謝謝提醒,是寫錯(cuò)了,i+48,從數(shù)字0開始。改過來了。如果有更好的解決方案,請跟貼。謝謝!
          2008-05-16 10:49 | 銀河使者

          # re: 一著名軟件公司的java筆試算法題的答案  回復(fù)  更多評論   

          public class DefTest
          {
          public static boolean validate(String s)
          {
          if (s.charAt(2) == '4')
          return false;
          if (s.indexOf("35") >= 0 || s.indexOf("53") >= 0)
          return false;
          return true;
          }

          public static void main(String[] ssdfa) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
          {
          cmp("","122345");
          }
          public static void cmp(String p,String ss)
          {
          if(ss.length() == 1)
          {
          if(validate(p+ss))
          System.out.println(p+ss);
          return;
          }
          for(int i=0;i<ss.length();i++)
          {
          if(ss.indexOf(ss.charAt(i)) == i)
          cmp(p+ss.charAt(i),ss.substring(0,i)+ss.substring(i+1, ss.length()));
          }
          }

          }
          2008-05-30 17:38 | walnutprince

          # re: 一著名軟件公司的java筆試算法題的答案  回復(fù)  更多評論   

          我前天才筆試過這道題,可惜沒做起!
          2009-03-05 20:01 | hbhjun

          # re: 一著名軟件公司的java筆試算法題的答案[未登錄]  回復(fù)  更多評論   

          你好,謝謝你的題目,我寫了另外的一個(gè)算法:http://www.aygfsteel.com/lanxiazhi/archive/2009/07/27/288626.html。解法有局限,只是針對這個(gè)題寫的。
          2009-07-27 20:08 | lanxiazhi

          # re: 一著名軟件公司的java筆試算法題的答案  回復(fù)  更多評論   

          using System;
          using System.Collections.Generic;
          using System.Text;
          using System.Collections;

          namespace ConsoleApplication1
          {
          class Program
          {
          static void Main(string[] args)
          {

          int[] x = new int[] { 1,2,2,3,4,5};
          Hashtable ht = new Hashtable();
          for (int a = 0; a < x.Length;a++ )
          {
          int[] outi=new int[6];

          outi[0] = x[a];

          for (int b = 0; b < x.Length;b++ )
          {
          if (b != a)
          {
          outi[1] = x[b];
          }
          else {
          continue;
          }



          for(int c=0;c<x.Length;c++){

          if (c != a && c != b && c != 4)
          {
          outi[2] = x[c];
          }
          else
          {
          continue;
          }


          for(int d=0;d<x.Length;d++){
          if (d != a && d != b && d != c)
          {
          outi[3] = x[d];
          }
          else
          {
          continue;
          }


          for (int e = 0; e < x.Length ;e++ )
          {
          if (e != a && e != b && e != c && e != d)
          {
          outi[4] = x[e];
          }
          else
          {

          continue;
          }


          for (int f = 0; f < x.Length ;f++ )
          {
          if (f != a && f != b && f != c && f != d && f != e)
          {
          outi[5] = x[f];
          }
          else
          {
          continue;
          }


          String outs = "";
          for (int z = 0; z < outi.Length;z++ )
          {
          outs += outi[z];

          }
          int aaa = -1;
          aaa = outs.IndexOf("35");
          int bbb = -1;
          bbb = outs.IndexOf("53");
          if (aaa ==-1 && bbb ==-1)
          {
          try
          {
          ht.Add(outs, outs);
          }catch(Exception fgh){
          continue;
          }
          }

          }

          }

          }

          }

          }
          }
          foreach(String i in ht.Keys){
          System.Console.WriteLine(i);
          }

          }
          }
          }
          腦子不行,弄了好久弄出這樣的垃圾,還用了哈希table= =
          2010-06-19 13:58 |

          # re: 一著名軟件公司的java筆試算法題的答案  回復(fù)  更多評論   

          .我看了這麼多個(gè),這個(gè)很完美.
          2010-09-14 17:57 | Lain

          # re: 一著名軟件公司的java筆試算法題的答案  回復(fù)  更多評論   

          博主,為什么在list方法里我如果不定義新變量s,而直接用result保存連接的結(jié)果,只能打印出123345來,暈了。
          2012-02-14 17:22 | theoffspring

          # re: 一著名軟件公司的java筆試算法題的答案  回復(fù)  更多評論   

          我這樣寫的,和你原理一樣,實(shí)現(xiàn)的略有不同

          public void list2(String usedIndex, String result) {
          for (int idx = 0; idx < arr.length; idx++) {
          if (usedIndex.indexOf(idx + 48) > -1) continue;
          result = result + String.valueOf(arr[idx]);

          if (result.length() == arr.length) {
          if (isValid(result)) {
          System.out.println(result);
          lastResult = result;
          cnt++;
          }

          break;
          }

          String newUsedIndex = usedIndex + String.valueOf(idx);
          list2(newUsedIndex, result);

          }

          }
          2012-02-14 17:23 | theoffspring
          主站蜘蛛池模板: 容城县| 义乌市| 平阴县| 平安县| 牟定县| 应城市| 康保县| 宿迁市| 龙陵县| 运城市| 衡东县| 迁西县| 青神县| 榆林市| 昌黎县| 原平市| 西乌珠穆沁旗| 绍兴市| 阳西县| 鄯善县| 杭锦旗| 宣武区| 榆树市| 隆德县| 泸州市| 逊克县| 土默特左旗| 南靖县| 社会| 通海县| 鄢陵县| 新巴尔虎左旗| 苗栗市| 潼南县| 太康县| 泾源县| 长子县| 淳安县| 星子县| 东乡县| 岳西县|