package com.itheima;
public class PermutCombi {
public static void output(int[] num, int[] arr) {
int i;
for (i = arr.length - 1; i >= 0; --i)
System.out.print((arr[num[i]]) + " ");
System.out.println();
}
public static void permutCombi(int[] arr, int n) {
int[] num, mark;
int i, flag;
num = new int[n + 1];
mark = new int[n];
for (i = 0; i <= n; ++i)
num[i] = 0;
do {
for (i = 0; i < n; ++i)
mark[i] = 0;
flag = 0;
++num[0];
for (i = 0; i < n; ++i) {
if (num[i] == n) {
num[i] = 0;
++num[i + 1];
}
++mark[num[i]];
// 各位数组单元有无重复
if (mark[num[i]] > 1) {
flag = 1;
break;
}
}
if (flag == 0)
output(num, arr);
} while (num[n] == 0);
}
public static void main(String[] args) {
int[] n = {12, 22, 2, 4, 6};
permutCombi(n, n.length);
}
}
运行结果:
12 22 2 4 6
12 22 2 6 4
12 22 4 2 6
12 22 4 6 2
12 22 6 2 4
12 22 6 4 2
12 2 22 4 6
12 2 22 6 4
12 2 4 22 6
12 2 4 6 22
12 2 6 22 4
12 2 6 4 22
12 4 22 2 6
12 4 22 6 2
12 4 2 22 6
12 4 2 6 22
12 4 6 22 2
12 4 6 2 22
12 6 22 2 4
12 6 22 4 2
12 6 2 22 4
12 6 2 4 22
12 6 4 22 2
12 6 4 2 22
22 12 2 4 6
22 12 2 6 4
22 12 4 2 6
22 12 4 6 2
22 12 6 2 4
22 12 6 4 2
22 2 12 4 6
22 2 12 6 4
22 2 4 12 6
22 2 4 6 12
22 2 6 12 4
22 2 6 4 12
22 4 12 2 6
22 4 12 6 2
22 4 2 12 6
22 4 2 6 12
22 4 6 12 2
22 4 6 2 12
22 6 12 2 4
22 6 12 4 2
22 6 2 12 4
22 6 2 4 12
22 6 4 12 2
22 6 4 2 12
2 12 22 4 6
2 12 22 6 4
2 12 4 22 6
2 12 4 6 22
2 12 6 22 4
2 12 6 4 22
2 22 12 4 6
2 22 12 6 4
2 22 4 12 6
2 22 4 6 12
2 22 6 12 4
2 22 6 4 12
2 4 12 22 6
2 4 12 6 22
2 4 22 12 6
2 4 22 6 12
2 4 6 12 22
2 4 6 22 12
2 6 12 22 4
2 6 12 4 22
2 6 22 12 4
2 6 22 4 12
2 6 4 12 22
2 6 4 22 12
4 12 22 2 6
4 12 22 6 2
4 12 2 22 6
4 12 2 6 22
4 12 6 22 2
4 12 6 2 22
4 22 12 2 6
4 22 12 6 2
4 22 2 12 6
4 22 2 6 12
4 22 6 12 2
4 22 6 2 12
4 2 12 22 6
4 2 12 6 22
4 2 22 12 6
4 2 22 6 12
4 2 6 12 22
4 2 6 22 12
4 6 12 22 2
4 6 12 2 22
4 6 22 12 2
4 6 22 2 12
4 6 2 12 22
4 6 2 22 12
6 12 22 2 4
6 12 22 4 2
6 12 2 22 4
6 12 2 4 22
6 12 4 22 2
6 12 4 2 22
6 22 12 2 4
6 22 12 4 2
6 22 2 12 4
6 22 2 4 12
6 22 4 12 2
6 22 4 2 12
6 2 12 22 4
6 2 12 4 22
6 2 22 12 4
6 2 22 4 12
6 2 4 12 22
6 2 4 22 12
6 4 12 22 2
6 4 12 2 22
6 4 22 12 2
6 4 22 2 12
6 4 2 12 22
6 4 2 22 12
希望对楼主有所帮助
|