public class 枚举排列 {
static int[] a = {0, 1, 2, 3};
static int result = 0;
static int selectNum = 2;
public static void main(String[] args) {
dfs(0,0);
System.out.println("The total of all permutation of a[] is: " + result);
}
public static void dfs(int start,int selected) {
if(selected==selectNum){
for (int i = 0; i < selectNum; i++) {
System.out.println(a[i]);
if(i<selectNum-1){
System.out.println(",");
}
}
System.out.println();
result++;
return;
}
for (int i = start; i < a.length; i++) {
swap(start, i);
dfs(start + 1,selectNum+1);
swap(start, i);
}
}
public static void swap(int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
枚举排列dfs
最新推荐文章于 2024-06-21 17:14:10 发布