AcWing 842. 排列数字
原题链接
简单
作者:
Broken_
,
2025-03-07 16:45:54
· 陕西
,
所有人可见
,
阅读 1
Java
import java.util.Scanner;
public class Main {
static int N = 10,n;
static int [] path = new int [N];
static boolean [] str = new boolean [N];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
dfs(0);
}
public static void dfs(int u) {
if(u == n){
for(int i = 0 ; i < n ; i++){
System.out.print(path[i]+" ");
}
System.out.println(" ");
return;
}
for(int i = 1 ; i <= n ; i++){
if(!str[i]){
path[u] = i;
str[i] = true;
dfs(u+1);
str[i] = false;
}
}
}
}