哪位大神可以撰寫0到9的八位數字的組合,并且不重復的,而且全部排列組合都輸出
uj5u.com熱心網友回復:
package algorithm.permutation;
import java.util.*;
public class Permutation {
/**
* 排列選擇(從串列中選擇n個排列)
* @param dataList 待選串列
* @param n 選擇個數
*/
public static void arrangementSelect(String[] dataList, int n) {
System.out.println(String.format("A(%d, %d) = %d", dataList.length, n, Util.arrangement(dataList.length, n)));
arrangementSelect(dataList, new String[n], 0);
}
/**
* 排列選擇
* @param dataList 待選串列
* @param resultList 前面(resultIndex-1)個的排列結果
* @param resultIndex 選擇索引,從0開始
*/
private static void arrangementSelect(String[] dataList, String[] resultList, int resultIndex) {
int resultLen = resultList.length;
if (resultIndex >= resultLen) { // 全部選擇完時,輸出排列結果
System.out.println(Arrays.asList(resultList));
return;
}
// 遞回選擇下一個
for (int i = 0; i < dataList.length; i++) {
// 判斷待選項是否存在于排列結果中
boolean exists = false;
for (int j = 0; j < resultIndex; j++) {
if (dataList[i].equals(resultList[j])) {
exists = true;
break;
}
}
if (!exists) { // 排列結果不存在該項,才可選擇
resultList[resultIndex] = dataList[i];
arrangementSelect(dataList, resultList, resultIndex + 1);
}
}
}
}
package algorithm.permutation;
import java.util.*;
public class Combination {
/**
* 組合選擇(從串列中選擇n個組合)
* @param dataList 待選串列
* @param n 選擇個數
*/
public static void combinationSelect(String[] dataList, int n) {
System.out.println(String.format("C(%d, %d) = %d", dataList.length, n, Util.combination(dataList.length, n)));
combinationSelect(dataList, 0, new String[n], 0);
}
public static void combinationSelect(Integer[] dataList, int n) {
String[] data = new String[dataList.length];
for(int i = 0; i < dataList.length; i++) {
data[i] = dataList[i] + "";
}
combinationSelect(data, n);
}
/**
* 組合選擇
* @param dataList 待選串列
* @param dataIndex 待選開始索引
* @param resultList 前面(resultIndex-1)個的組合結果
* @param resultIndex 選擇索引,從0開始
*/
private static void combinationSelect(String[] dataList, int dataIndex, String[] resultList, int resultIndex) {
int resultLen = resultList.length;
int resultCount = resultIndex + 1;
if (resultCount > resultLen) { // 全部選擇完時,輸出組合結果
System.out.println(Arrays.asList(resultList));
return;
}
// 遞回選擇下一個
for (int i = dataIndex; i < dataList.length + resultCount - resultLen; i++) {
resultList[resultIndex] = dataList[i];
combinationSelect(dataList, i + 1, resultList, resultIndex + 1);
}
}
}
package algorithm.permutation;
public class Util {
/**
* 計算階乘數,即n! = n * (n-1) * ... * 2 * 1
* @param n
* @return
*/
private static long factorial(int n) {
return (n > 1) ? n * factorial(n - 1) : 1;
}
/**
* 計算排列數,即A(n, m) = n!/(n-m)!
* @param n
* @param m
* @return
*/
public static long arrangement(int n, int m) {
return (n >= m) ? factorial(n) / factorial(n - m) : 0;
}
/**
* 計算組合數,即C(n, m) = n!/((n-m)! * m!)
* @param n
* @param m
* @return
*/
public static long combination(int n, int m) {
return (n >= m) ? 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", "8","9" }, 8);
Combination.combinationSelect(new String[] { "1", "2", "3", "4", "5", "6", "7", "8","9" }, 8);
}
}
排列控制臺應該打不下,你可以用流輸出到文本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/122382.html
標籤:Java相關
上一篇:問一個服務器獲取手機端的IP判別
下一篇:萌新小白求救各位大佬!!!!1
