package aha;
//dfs
import java.util.Scanner;
public class quanpai {
static int book[] = new int[100];// static int book[];错误
static int a[] = new int[100];
static int n;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
dfs(1);
}
private static void dfs(int step) {
if (step == n + 1) {
for (int i = 1; i <= n; i++) {
System.out.print(a[i] + " ");
}
System.out.println();
return;//返回最近调用dfs的 地方,不然死循环
}
for (int i = 1; i <= n; i++) {
if (book[i] == 0) {
book[i] = 1;
a[step] = i;
dfs(step + 1);
book[i] = 0;
}
}
return;//
}
}
全排列-深搜问题
最新推荐文章于 2022-07-14 20:52:17 发布