用JAVA写一个a,b,c,d,e排列组合算法,谢谢了
2个回答
展开全部
public class Paixu {
public static void main(String[] args) {
char[] in = "abcde".toCharArray();
new Paixu().paixu(in, in.length, 0);
}
private void paixu(char[] array, int n, int k) {
if (n == k) {
char[] out = new char[n];
for (int i = 0; i < array.length; i++) {
out[i] = array[i];
}
System.out.println(new String(out));
} else {
for (int i = k; i < n; i++) {
swap(array, k, i);
paixu(array, n, k + 1);
swap(array, i, k);
}
}
}
private void swap(char[] a, int x, int y) {
char temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
public static void main(String[] args) {
char[] in = "abcde".toCharArray();
new Paixu().paixu(in, in.length, 0);
}
private void paixu(char[] array, int n, int k) {
if (n == k) {
char[] out = new char[n];
for (int i = 0; i < array.length; i++) {
out[i] = array[i];
}
System.out.println(new String(out));
} else {
for (int i = k; i < n; i++) {
swap(array, k, i);
paixu(array, n, k + 1);
swap(array, i, k);
}
}
}
private void swap(char[] a, int x, int y) {
char temp = a[x];
a[x] = a[y];
a[y] = temp;
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询