代碼實現排列組合【Java】


一.代碼實現  

  1 package zhen;
  2 
  3 import java.util.Arrays;
  4 
  5 public class Arrangement {
  6 
  7       /**
  8      * 計算階乘數,即n! = n * (n-1) * ... * 2 * 1 
  9      */
 10     private static long factorial(int n) {
 11         long sum = 1;
 12         while( n > 0 ) {
 13             sum = sum * n--;
 14         }
 15         return sum;
 16     }
 17     
 18       /**
 19      * 排列計算公式A = n!/(n - m)!
 20      */
 21     public static long arrangement(int m, int n) {
 22         return m <= n ? factorial(n) / factorial(n - m) : 0;
 23     }
 24     
 25      /**
 26      * 排列選擇(從列表中選擇n個排列) 
 27      * @param dataList 待選列表 
 28      * @param n 選擇個數 
 29      */
 30     public static void arrangementSelect(String[] dataList, int n) {
 31         System.out.println(String.format("A(%d, %d) = %d", dataList.length, n, arrangement(n, dataList.length)));
 32         arrangementSelect(dataList, new String[n], 0);
 33     }
 34 
 35     /** 
 36      * 排列選擇 
 37      * @param dataList 待選列表 
 38      * @param resultList 前面(resultIndex-1)個的排列結果 
 39      * @param resultIndex 選擇索引,從0開始 
 40      */  
 41     private static void arrangementSelect(String[] dataList, String[] resultList, int resultIndex) {
 42         int resultLen = resultList.length;
 43         if(resultIndex >= resultLen) { // 全部選擇完時,輸出排列結果 
 44             System.out.println(Arrays.asList(resultList));
 45             return;
 46         }
 47 
 48         // 遞歸選擇下一個
 49         for(int i = 0; i < dataList.length; i++) {
 50             // 判斷待選項是否存在於排列結果中
 51             boolean exists = false;
 52             for (int j = 0; j < resultIndex; j++) {
 53                 if (dataList[i].equals(resultList[j])) {
 54                     exists = true;
 55                     break;
 56                 }
 57             }
 58             if (!exists) { // 排列結果不存在該項,才可選擇
 59                 resultList[resultIndex] = dataList[i];
 60                 arrangementSelect(dataList, resultList, resultIndex + 1);
 61             }
 62         }
 63     }
 64     
 65     /**
 66      * 組合計算公式C<sup>m</sup><sub>n</sub> = n! / (m! * (n - m)!)
 67      * @param m
 68      * @param n
 69      * @return
 70      */
 71     public static long combination(int m, int n) {
 72         return m <= n ? factorial(n) / (factorial(m) * factorial((n - m))) : 0;
 73     }
 74     
 75     /**
 76      * 組合選擇(從列表中選擇n個組合)
 77      * @param dataList 待選列表
 78      * @param n 選擇個數
 79      */
 80     public static void combinationSelect(String[] dataList, int n) {
 81         System.out.println(String.format("C(%d, %d) = %d", dataList.length, n, combination(n, dataList.length)));
 82         combinationSelect(dataList, 0, new String[n], 0);
 83     }
 84 
 85     /**
 86      * 組合選擇
 87      * @param dataList 待選列表
 88      * @param dataIndex 待選開始索引
 89      * @param resultList 前面(resultIndex-1)個的組合結果
 90      * @param resultIndex 選擇索引,從0開始
 91      */
 92     private static void combinationSelect(String[] dataList, int dataIndex, String[] resultList, int resultIndex) {  
 93         int resultLen = resultList.length;
 94         int resultCount = resultIndex + 1;
 95         if (resultCount > resultLen) { // 全部選擇完時,輸出組合結果
 96             System.out.println(Arrays.asList(resultList));
 97             return;
 98         }
 99 
100         // 遞歸選擇下一個
101         for (int i = dataIndex; i < dataList.length + resultCount - resultLen; i++) {
102             resultList[resultIndex] = dataList[i];
103             combinationSelect(dataList, i + 1, resultList, resultIndex + 1);
104         }
105     }
106     
107     public static void main(String[] args) {
108         String[] array = new String[4];
109 
110         array[0] = "SG614111010000000010001";
111         array[1] = "SG614111020000000020001";
112         array[2] = "SG614111030000000030001";
113         array[3] = "SG614111040000000040001";
114         /**
115          * 測試排列
116          */
117         System.out.println("測試排列:");
118         arrangementSelect(array, array.length);
119         
120         /**
121          * 測試組合
122          */
123         System.out.println("測試組合:");
124         for(int i = 1; i <= array.length; i++){
125             combinationSelect(array, i);
126         }
127     }
128 }

二.結果

 1 測試排列:
 2 A(4, 4) = 24
 3 [SG614111010000000010001, SG614111020000000020001, SG614111030000000030001, SG614111040000000040001]
 4 [SG614111010000000010001, SG614111020000000020001, SG614111040000000040001, SG614111030000000030001]
 5 [SG614111010000000010001, SG614111030000000030001, SG614111020000000020001, SG614111040000000040001]
 6 [SG614111010000000010001, SG614111030000000030001, SG614111040000000040001, SG614111020000000020001]
 7 [SG614111010000000010001, SG614111040000000040001, SG614111020000000020001, SG614111030000000030001]
 8 [SG614111010000000010001, SG614111040000000040001, SG614111030000000030001, SG614111020000000020001]
 9 [SG614111020000000020001, SG614111010000000010001, SG614111030000000030001, SG614111040000000040001]
10 [SG614111020000000020001, SG614111010000000010001, SG614111040000000040001, SG614111030000000030001]
11 [SG614111020000000020001, SG614111030000000030001, SG614111010000000010001, SG614111040000000040001]
12 [SG614111020000000020001, SG614111030000000030001, SG614111040000000040001, SG614111010000000010001]
13 [SG614111020000000020001, SG614111040000000040001, SG614111010000000010001, SG614111030000000030001]
14 [SG614111020000000020001, SG614111040000000040001, SG614111030000000030001, SG614111010000000010001]
15 [SG614111030000000030001, SG614111010000000010001, SG614111020000000020001, SG614111040000000040001]
16 [SG614111030000000030001, SG614111010000000010001, SG614111040000000040001, SG614111020000000020001]
17 [SG614111030000000030001, SG614111020000000020001, SG614111010000000010001, SG614111040000000040001]
18 [SG614111030000000030001, SG614111020000000020001, SG614111040000000040001, SG614111010000000010001]
19 [SG614111030000000030001, SG614111040000000040001, SG614111010000000010001, SG614111020000000020001]
20 [SG614111030000000030001, SG614111040000000040001, SG614111020000000020001, SG614111010000000010001]
21 [SG614111040000000040001, SG614111010000000010001, SG614111020000000020001, SG614111030000000030001]
22 [SG614111040000000040001, SG614111010000000010001, SG614111030000000030001, SG614111020000000020001]
23 [SG614111040000000040001, SG614111020000000020001, SG614111010000000010001, SG614111030000000030001]
24 [SG614111040000000040001, SG614111020000000020001, SG614111030000000030001, SG614111010000000010001]
25 [SG614111040000000040001, SG614111030000000030001, SG614111010000000010001, SG614111020000000020001]
26 [SG614111040000000040001, SG614111030000000030001, SG614111020000000020001, SG614111010000000010001]
27 測試組合:
28 C(4, 1) = 4
29 [SG614111010000000010001]
30 [SG614111020000000020001]
31 [SG614111030000000030001]
32 [SG614111040000000040001]
33 C(4, 2) = 6
34 [SG614111010000000010001, SG614111020000000020001]
35 [SG614111010000000010001, SG614111030000000030001]
36 [SG614111010000000010001, SG614111040000000040001]
37 [SG614111020000000020001, SG614111030000000030001]
38 [SG614111020000000020001, SG614111040000000040001]
39 [SG614111030000000030001, SG614111040000000040001]
40 C(4, 3) = 4
41 [SG614111010000000010001, SG614111020000000020001, SG614111030000000030001]
42 [SG614111010000000010001, SG614111020000000020001, SG614111040000000040001]
43 [SG614111010000000010001, SG614111030000000030001, SG614111040000000040001]
44 [SG614111020000000020001, SG614111030000000030001, SG614111040000000040001]
45 C(4, 4) = 1
46 [SG614111010000000010001, SG614111020000000020001, SG614111030000000030001, SG614111040000000040001]

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM