AcWing 842. 排列数字 Java
原题链接
简单
作者:
leo_0
,
2020-03-17 14:34:47
,
所有人可见
,
阅读 560
题目描述
算法1
Java 代码
import java.util.Scanner;
public class Main {
static int n;
static int[] path;
static boolean[] st;
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(!st[i]) {
path[u] = i;
st[i] = true;
dfs(u + 1);
st[i] = false;
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
path = new int[n];
st = new boolean[n+1];
dfs(0);
}
}