CodePudding user response:
Package algorithm. Permutation;
Import the Java. Util. *;
Public class Permutation {
/* *
* permutation choose (select from the list n)
* @ param dataList proposed list
* @ param number n choose
*/
Public static void arrangementSelect (String [] dataList, int n) {
System. Out. Println (the String. Format (" A=% d (% d, % d) ", the dataList. Length, n, Util. Arrangement (dataList. Length, n)));
ArrangementSelect (dataList, new String [n], 0).
}
/* *
* permutation choose
* @ param dataList proposed list
* @ param resultList front (resultIndex - 1) the arrangement of the results
* @ param resultIndex selection index, starting from 0
*/
Private static void arrangementSelect (String [] dataList, String [] resultList, int resultIndex) {
Int resultLen=resultList. Length;
If (resultIndex & gt;=resultLen) {//all select, arrange output the results
System. The out. Println (arrays.aslist (resultList));
return;
}
//recursive select next
for (int i=0; I & lt; DataList. Length; I++) {
//determine whether stay option exists in arrangement results
boolean exists=false;
For (int j=0; J & lt; ResultIndex; J++) {
If (dataList [I] equals (resultList) [j]) {
exists=true;
break;
}
}
if (! The exists) {//there is no the arrangement result, only can choose
ResultList [resultIndex]=dataList [I];
ArrangementSelect (dataList, resultList resultIndex + 1);
}
}
}
}
Package algorithm. Permutation;
Import the Java. Util. *;
Public class Combination {
/* *
* portfolio selection (select from the list n combination)
* @ param dataList proposed list
* @ param number n choose
*/
Public static void combinationSelect (String [] dataList, int n) {
System. Out. Println (the String. Format (" C (% d, % d)=% d ", dataList. Length, n, Util.com bination (dataList. Length, n)));
CombinationSelect (dataList, 0, new String [n], 0).
}
Public static void combinationSelect (Integer [] dataList, int n) {
Data=https://bbs.csdn.net/topics/new String String [] [dataList. Length];
for(int i=0; I & lt; DataList. Length; I++) {
Data [I]=dataList [I] + "";
}
CombinationSelect (data, n);
}
/* *
* options
* @ param dataList proposed list
* @ param dataIndex to choose the starting index
* @ param resultList front (resultIndex - 1), a combination of the results
* @ param resultIndex selection index, starting from 0
*/
Private static void combinationSelect (String [] dataList, int dataIndex, String [] resultList, int resultIndex) {
Int resultLen=resultList. Length;
Int resultCount=resultIndex + 1;
If (resultCount & gt; ResultLen) {//select all, output combination results
System. The out. Println (arrays.aslist (resultList));
return;
}
//recursive select next
For (int I=dataIndex; I & lt; The dataList. Length + resultCount - resultLen; I++) {
ResultList [resultIndex]=dataList [I];
CombinationSelect (dataList, I + 1, resultList, resultIndex + 1);
}
}
}
Package algorithm. Permutation;
Public class Util {
/* *
* calculation of factorial, i.e., n!=n * (n - 1) *... * 2 * 1
* @ param n
* @ return
*/
Private static long factorial (int n) {
Return (n & gt; 1)? N * factorial (n - 1) : 1;
}
/* *
* calculation of arrangement, that is, A (n, m)=n!/(n - m)!
* @ param n
* @ param m
* @ return
*/
Public static long arrangement (int n, int m) {
Return (n & gt;=m)? The factorial (n)/factorial (n, m) : 0;
}
/* *
* calculation of combination, namely C (n, m)=n!/((n, m)! * m!)
* @ param n
* @ param m
* @ return
*/
Public static long combination (int n, int m) {
Return (n & gt;=m)? The factorial (n)/factorial (n, m)/factorial (m) : 0;
}
}
Package algorithm. Permutation;
Public class TestPAndC {
Public static void main (String [] args) {
Permutation. ArrangementSelect (new String [] {" 1 ", "2", "3", "4", "5", "6", "7", "eight" and "9"}, 8).
Combination.com binationSelect (new String [] {" 1 ", "2", "3", "4", "5", "6", "7", "eight" and "9"}, 8).
}
}
Arrangement of the console should play not bottom, you can use the current output to a text,