【面試程式】輸出排列組合的數字

來源:互聯網
上載者:User

***************************************
題目:
輸出所有的組合用1,2,3,4,5組合的數字

想法:遞迴,先挑選出一個數字,然後,剩下的繼續進行組合。
直到只剩下兩個數位時候,交換,輸出。

代碼:
private void wapOut(String change,String stati){
         if (change.length() == 2){
             String temp = String.valueOf(stati);
             System.out.println(temp+change);
             System.out.println(temp+change.charAt(1)+change.charAt(0));
         }else{
             for(int i=0; i<change.length(); i++){
                 String temp = change.substring(0,i)+change.substring(i+1,change.length());
                 wapOut(temp,stati+change.substring(i,i+1));
             }
         }
     }

測試:
wapOut("1234",""),輸出24個數字
wapOut("12345",""),輸出120個數字
***************************************
題目:
用1,2,2,3,4,5,這六個數字,用java寫一個main函數,列印出所有不同的排列,如:512234,212345等,
要求:“4”不能排第3位,“1”與“5”不能相連。

思路:先輸出所有的組合,然後剔除其中的不符合要求的.

代碼:
int num[] = {1, 2, 2, 3, 4, 5};
     int total = 0;

     private void wapB() {
         int len = num.length;
         for (int i0 = 0; i0 < len; i0++) {
             for (int i1 = 0; i1 < len; i1++) {
                 for (int i2 = 0; i2 < len; i2++) {
                     for (int i3 = 0; i3 < len; i3++) {
                         for (int i4 = 0; i4 < len; i4++) {
                             for (int i5 = 0; i5 < len; i5++) {
                                 int n[] = new int[]{i0,i1,i2,i3,i4,i5};
                                 if (Same(n) || RepeatOf2(n) || ThirdOf4(n) || Separate15(n)){
                                     continue;
                                 }
                                 GetNum(n);
                             }
                         }
                     }
                 }
             }
         }
     }
    
     //有數字重複,例如11111
     boolean Same(int []n){
         for (int i=0; i<n.length; i++){
             for(int j=i+1; j<n.length; j++){
                 if (n[i] == n[j]){
                     return true;
                 }
             }
         }
         return false;
     }
    
     //因為有兩個2,所以要減少一次重複 比如 122345 和 122345
     boolean RepeatOf2(int []n){
         for (int i=0; i<n.length; i++){
             for(int j=i+1; j<n.length; j++){
                 if ((num[n[i]] == num[n[j]]) && (num[n[i]] == 2) && (n[i]>n[j])){
                     return true;
                 }
             }
         }
         return false;
     }
    
     //要求
     boolean ThirdOf4(int []n){
         if (num[n[3]] == 4){
             return true;
         }
         return false;
     }
    
     //要求
     boolean Separate15(int []n){
         for (int i=0; i<n.length; i++){
            for(int j=i+1; j<n.length; j++){
                if ((num[n[i]] == 1 && num[n[i+1]] == 5) || (num[n[i]] == 5 && num[n[i+1]] == 1)){
                    return true;
                }
            }
        }

         return false;
     }
    
     String GetNum(int[] n){
         String temp = "";
         for (int i=0; i<n.length; i++){
             temp += String.valueOf(num[n[i]]);
         }
         total++;
         System.out.println(temp);
         return temp;
     }

測試:
結果是198,符合.

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.