public class test {
//传入原数组;
int[] a;
//数组长度,即个数;
final int N;
//第三方原数组常量;
final int[] temp;
//记录每一层的上一层初始排列;
int[][] b;
//记录排列组合个数;
int count = 0;
int[] a;
//数组长度,即个数;
final int N;
//第三方原数组常量;
final int[] temp;
//记录每一层的上一层初始排列;
int[][] b;
//记录排列组合个数;
int count = 0;
//用于存储结果的数组的行数;
int r;
//记录存储数组时的行标;
int s1 = 0;
//存储数组
public int[][] code;
int r;
//记录存储数组时的行标;
int s1 = 0;
//存储数组
public int[][] code;
/**
* 初始化;
*/
test(int[] a) {
this.a = a;
this.N = a.length;
this.temp = new int[N];
System.arraycopy(a, 0, temp, 0, a.length);
this.b = new int[N][N];
this.r = factorialCount(N);
this.count=r;
this.code = new int[r][N];
}
* 初始化;
*/
test(int[] a) {
this.a = a;
this.N = a.length;
this.temp = new int[N];
System.arraycopy(a, 0, temp, 0, a.length);
this.b = new int[N][N];
this.r = factorialCount(N);
this.count=r;
this.code = new int[r][N];
}
/**
* 计算排列组合有多少种
* @param x 原数组元素个数
* @return
*/
private int factorialCount(int x) {
if (x > 1)
return x * factorialCount(x - 1);
else
return 1;
}
/**
* 打印结果
*/
public void print() {
for (int i = 0; i < code.length; i++) {
for (int j = 0; j < code[i].length; j++)
System.out.print(code[i][j] + " ");
System.out.println();
}
* 打印结果
*/
public void print() {
for (int i = 0; i < code.length; i++) {
for (int j = 0; j < code[i].length; j++)
System.out.print(code[i][j] + " ");
System.out.println();
}
System.out.println("排列组合共: " + count + "种");
}
}
/**
* 核心变换
* @param x 元素下标
*/
public void factorial(int x) {
if (x < N) {
for (int i = x; i < N; i++) {
if (x > 0)
for (int h = x; h < N; h++)
a[h] = b[x - 1][h];
else
for (int h = x; h < N; h++)
a[h] = temp[h];
for (int i = x; i < N; i++) {
if (x > 0)
for (int h = x; h < N; h++)
a[h] = b[x - 1][h];
else
for (int h = x; h < N; h++)
a[h] = temp[h];
int t;
t = a[x];
a[x] = a[i];
a[i] = t;
t = a[x];
a[x] = a[i];
a[i] = t;
for (int h = 0; h < N; h++)
b[x][h] = a[h];
b[x][h] = a[h];
if (x < N - 1)
factorial(x + 1);
else {
for (int j = 0; j < N; j++) {
code[s1][j] = a[j];
}
s1++;
factorial(x + 1);
else {
for (int j = 0; j < N; j++) {
code[s1][j] = a[j];
}
s1++;
}
}
}
}
}
}
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
* 测试
* @param args
*/
public static void main(String[] args) {
int[] a = { 1, 2, 3};
test t = new test(a);
t.factorial(0);
//打印结果;
t.print();
test t = new test(a);
t.factorial(0);
//打印结果;
t.print();
}
}
}