发布网友 发布时间:2022-05-05 20:59
共1个回答
热心网友 时间:2022-06-28 02:25
import java.util.Scanner;
public class T {
public static void main(String[] args) {
System.out.println("请输入一个正整数:");
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
Integer[] list = new Integer[n];
System.out.printf("请输入%d个不相同的整数:\n", n);
for (int i = 0; i < n; i++) {
list[i] = scanner.nextInt();
}
scanner.close();
System.out.println();
System.out.println("全排列:");
perm(list, 0, n);
}
public static void perm(Object[] list, int k, int m) {
if (k == m) {
for (int i = 0; i < m; i++)
System.out.print(list[i] + " ");
System.out.println();
} else {
for (int i = k; i < m; i++) {
swap(list, k, i);
perm(list, k + 1, m);
swap(list, k, i);
}
}
}
private static void swap(Object[] list, int k, int i) {
Object temp = list[k];
list[k] = list[i];
list[i] = temp;
}
}