mysql排列组合实现_Java实现数列的排列组合
定義:
排列:從給定個(gè)數(shù)的元素中取出指定個(gè)數(shù)的元素,進(jìn)行排序
組合:從給定個(gè)數(shù)的元素中僅取出指定個(gè)數(shù)的元素,不考慮排序
公式:
從n個(gè)元素中取出m個(gè)元素進(jìn)行排序的個(gè)數(shù):
A(m,n)=n*(n-1)(n-2)...*(n-m+1)=n!/(n-m)!
從n個(gè)元素中取出m個(gè)元素進(jìn)行組合的個(gè)數(shù):
C(m,n)=n!/[m!*(n-m)!]
注意:
0!=1
代碼實(shí)現(xiàn):
計(jì)算階乘,排列數(shù),組合數(shù)
/**
* 計(jì)算n的階乘:n! = n * (n-1) * (n-2) * ... *2 * 1
*/
public static long factorial(int n){
return (n>1) ? n*factorial(n-1) : 1;
}
/**
* 計(jì)算排列數(shù):A(n, m) = n!/(n-m)! -- 從n個(gè)數(shù)中取出m個(gè)數(shù)進(jìn)行排列 ,需要考慮數(shù)的順序 (如果n個(gè)數(shù)進(jìn)行排列,有n!種情況)
*/
public static long arrangement(int n, int m){
return (n >= m) ? factorial(n)/factorial(n-m) : 0;
}
/**
* 計(jì)算組合數(shù):C(n, m) = n!/((n-m)! * m!) -- 從n個(gè)數(shù)中取出m個(gè)數(shù)進(jìn)行排列 ,不考慮數(shù)的順序 (如 1234 和 4321 屬于一種組合,都包含1,2,3,4這四個(gè)數(shù))
*/
public static long combination(int m, int n){
return (n >= m) ? factorial(n)/(factorial(n-m)*factorial(m)) : 0;
}
窮舉出所有的排列結(jié)果
/**
* 排列:從數(shù)組a中選擇n個(gè)數(shù)進(jìn)行排列
*/
public static void arrangementSelect(int[] a, int n){
System.out.println(String.format("A(%d, %d) = %d", a.length, n, arrangement(a.length, n)));
arrangementSort(a, new int[n], 0);
}
/**
* 通過(guò)遞歸的方式羅列出所有的排列結(jié)果
* @param a:初始數(shù)組
* @param result:排列數(shù)組初始狀態(tài)
* @param resultIndex:比較的起始索引
*/
public static void arrangementSort(int[] a, int[] result, int resultIndex){
int result_length = result.length;
if(resultIndex >= result_length){
System.out.println(Arrays.toString(result)); // 輸出排列結(jié)果
return;
}
for(int i=0; i
// 判斷待選的數(shù)是否存在于排列的結(jié)果中
boolean exist = false;
for(int j=0; j
if(a[i] == result[j]){ // 若已存在,則不能重復(fù)選
exist = true;
break;
}
}
if(!exist){ // 若不存在,則可以選擇
result[resultIndex] = a[i];
arrangementSort(a, result, resultIndex+1);
}
}
}
窮舉出所有的組合結(jié)果
/**
* 組合:從數(shù)組a中選擇n個(gè)數(shù)進(jìn)行組合
*/
public static void combinationSelect(int a[], int n){
System.out.println(String.format("C(%d, %d)= %d", a.length, n, combination(a.length, n)));
combinationSort(a, 0, new int[a.length], 0);
}
/**
* 通過(guò)遞歸的方式羅列出所有的組合結(jié)果
* @param a:初始數(shù)組
* @param a_index:初始數(shù)組起始下標(biāo)
* @param result:初始組合數(shù)組
* @param r_index:初始組合數(shù)組的起始下標(biāo)
*/
public static void combinationSort(int[] a, int a_index, int[] result, int r_index){
int r_len = result.length;
int r_count = r_index + 1;
if(r_count > r_len){
System.out.println(Arrays.toString(result)); // 輸出組合結(jié)果
return;
}
for(int i=a_index; i
result[r_index] = a[i];
combinationSort(a, i+1, result, r_index+1);
}
}
完整代碼
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] a = {1, 2, 3, 4}; // 初始數(shù)組
arrangementSelect(a, 4);
combinationSelect(a, 3);
}
/**
* 計(jì)算n的階乘:n! = n * (n-1) * (n-2) * ... *2 * 1
*/
public static long factorial(int n){
return (n>1) ? n*factorial(n-1) : 1;
}
/**
* 計(jì)算排列數(shù):A(n, m) = n!/(n-m)! -- 從n個(gè)數(shù)中取出m個(gè)數(shù)進(jìn)行排列 ,需要考慮數(shù)的順序 (如果n個(gè)數(shù)進(jìn)行排列,有n!種情況)
*/
public static long arrangement(int n, int m){
return (n >= m) ? factorial(n)/factorial(n-m) : 0;
}
/**
* 計(jì)算組合數(shù):C(n, m) = n!/((n-m)! * m!) -- 從n個(gè)數(shù)中取出m個(gè)數(shù)進(jìn)行排列 ,不考慮數(shù)的順序 (如 1234 和 4321 屬于一種組合,都包含1,2,3,4這四個(gè)數(shù))
*/
public static long combination(int m, int n){
return (n >= m) ? factorial(n)/(factorial(n-m)*factorial(m)) : 0;
}
/**
* 排列:從數(shù)組a中選擇n個(gè)數(shù)進(jìn)行排列
*/
public static void arrangementSelect(int[] a, int n){
System.out.println(String.format("A(%d, %d) = %d", a.length, n, arrangement(a.length, n)));
arrangementSort(a, new int[n], 0);
}
/**
* 通過(guò)遞歸的方式羅列出所有的排列結(jié)果
* @param a:初始數(shù)組
* @param result:排列數(shù)組初始狀態(tài)
* @param resultIndex:比較的起始索引
*/
public static void arrangementSort(int[] a, int[] result, int resultIndex){
int result_length = result.length;
if(resultIndex >= result_length){
System.out.println(Arrays.toString(result)); // 輸出排列結(jié)果
return;
}
//
for(int i=0; i
// 判斷待選的數(shù)是否存在于排列的結(jié)果中
boolean exist = false;
for(int j=0; j
if(a[i] == result[j]){ // 若已存在,則不能重復(fù)選
exist = true;
break;
}
}
if(!exist){ // 若不存在,則可以選擇
result[resultIndex] = a[i];
arrangementSort(a, result, resultIndex+1);
}
}
}
/**
* 組合:從數(shù)組a中選擇n個(gè)數(shù)進(jìn)行組合
*/
public static void combinationSelect(int a[], int n){
System.out.println(String.format("C(%d, %d)= %d", a.length, n, combination(a.length, n)));
combinationSort(a, 0, new int[a.length], 0);
}
/**
* 通過(guò)遞歸的方式羅列出所有的組合結(jié)果
* @param a:初始數(shù)組
* @param a_index:初始數(shù)組起始下標(biāo)
* @param result:初始組合數(shù)組
* @param r_index:初始組合數(shù)組的起始下標(biāo)
*/
public static void combinationSort(int[] a, int a_index, int[] result, int r_index){
int r_len = result.length;
int r_count = r_index + 1;
if(r_count > r_len){
System.out.println(Arrays.toString(result)); // 輸出組合結(jié)果
return;
}
for(int i=a_index; i
result[r_index] = a[i];
combinationSort(a, i+1, result, r_index+1);
}
}
}
運(yùn)行結(jié)果:
A(4, 4) = 24
[1, 2, 3, 4]
[1, 2, 4, 3]
[1, 3, 2, 4]
[1, 3, 4, 2]
[1, 4, 2, 3]
[1, 4, 3, 2]
[2, 1, 3, 4]
[2, 1, 4, 3]
[2, 3, 1, 4]
[2, 3, 4, 1]
[2, 4, 1, 3]
[2, 4, 3, 1]
[3, 1, 2, 4]
[3, 1, 4, 2]
[3, 2, 1, 4]
[3, 2, 4, 1]
[3, 4, 1, 2]
[3, 4, 2, 1]
[4, 1, 2, 3]
[4, 1, 3, 2]
[4, 2, 1, 3]
[4, 2, 3, 1]
[4, 3, 1, 2]
[4, 3, 2, 1]
C(4, 3)= 0
[1, 2, 3, 4]
Process finished with exit code 0
總結(jié)
以上是生活随笔為你收集整理的mysql排列组合实现_Java实现数列的排列组合的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: django设置mysql数据库连接_d
- 下一篇: mysql数据回滚占用id吗_mysql