本文實例為大家分享了java排列組合算法的具體代碼,供大家參考,具體內容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
package BeanUtil; import java.util.ArrayList; import java.util.List; import com.work.core.exception.OurException; /** * 統計任三出現的最多的幾率的組合 * * @author wangmingjie * @date 2009-1-1下午01:22:19 */ public class Copy_2_of_StatisAnyThree { // 組合算法 // 本程序的思路是開一個數組,其下標表示1到m個數,數組元素的值為1表示其下標 // 代表的數被選中,為0則沒選中。 // 首先初始化,將數組前n個元素置1,表示第一個組合為前n個數。 // 然后從左到右掃描數組元素值的“10”組合,找到第一個“10”組合后將其變為 // “01”組合,同時將其左邊的所有“1”全部移動到數組的最左端。 // 當第一個“1”移動到數組的m-n的位置,即n個“1”全部移動到最右端時,就得 // 到了最后一個組合。 // 例如求5中選3的組合: // 1 1 1 0 0 //1,2,3 // 1 1 0 1 0 //1,2,4 // 1 0 1 1 0 //1,3,4 // 0 1 1 1 0 //2,3,4 // 1 1 0 0 1 //1,2,5 // 1 0 1 0 1 //1,3,5 // 0 1 1 0 1 //2,3,5 // 1 0 0 1 1 //1,4,5 // 0 1 0 1 1 //2,4,5 // 0 0 1 1 1 //3,4,5 public static void main(String[] args) { Copy_2_of_StatisAnyThree s = new Copy_2_of_StatisAnyThree(); s.printAnyThree(); } /** * */ public void printAnyThree(){ int [] num = new int []{ 1 , 2 , 3 , 4 , 5 , 6 }; print(combine(num, 3 )); } /** * 從n個數字中選擇m個數字 * @param a * @param m * @return */ public List combine( int [] a, int m){ int n = a.length; if (m>n){ throw new OurException( "錯誤!數組a中只有" +n+ "個元素。" +m+ "大于" + 2 + "!!!" ); } List result = new ArrayList(); int [] bs = new int [n]; for ( int i= 0 ;i<n;i++){ bs[i]= 0 ; } //初始化 for ( int i= 0 ;i<m;i++){ bs[i]= 1 ; } boolean flag = true ; boolean tempFlag = false ; int pos = 0 ; int sum = 0 ; //首先找到第一個10組合,然后變成01,同時將左邊所有的1移動到數組的最左邊 do { sum = 0 ; pos = 0 ; tempFlag = true ; result.add(print(bs,a,m)); for ( int i= 0 ;i<n- 1 ;i++){ if (bs[i]== 1 && bs[i+ 1 ]== 0 ){ bs[i]= 0 ; bs[i+ 1 ]= 1 ; pos = i; break ; } } //將左邊的1全部移動到數組的最左邊 for ( int i= 0 ;i<pos;i++){ if (bs[i]== 1 ){ sum++; } } for ( int i= 0 ;i<pos;i++){ if (i<sum){ bs[i]= 1 ; } else { bs[i]= 0 ; } } //檢查是否所有的1都移動到了最右邊 for ( int i= n-m;i<n;i++){ if (bs[i]== 0 ){ tempFlag = false ; break ; } } if (tempFlag== false ){ flag = true ; } else { flag = false ; } } while (flag); result.add(print(bs,a,m)); return result; } private int [] print( int [] bs, int [] a, int m){ int [] result = new int [m]; int pos= 0 ; for ( int i= 0 ;i<bs.length;i++){ if (bs[i]== 1 ){ result[pos]=a[i]; pos++; } } return result ; } private void print(List l){ for ( int i= 0 ;i<l.size();i++){ int [] a = ( int [])l.get(i); for ( int j= 0 ;j<a.length;j++){ System.out.print(a[j]+ "/t" ); } System.out.println(); } } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/wmj2003/article/details/3678941