代码实现排列组合【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]
原文地址:https://www.cnblogs.com/yszd/p/10902549.html