AcWing 94. 递归实现排列型枚举
原题链接
简单
Java 代码
import java.util.Scanner;
public class Main {
static int n;
static int[] path = new int[10];
static boolean[] state = new boolean[10];
static void dfs(int num){
if(num>n){
for (int i = 1; i <= n; i++) {
if(state[i])
System.out.print(path[i] + " ");
}
System.out.println();
return;
}
for (int i = 1; i <= n; i++) {
if(!state[i]){
state[i] = true;
path[num] = i;
dfs(num+1);
state[i] =false;
}
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
n = scanner.nextInt();
dfs(1);
}
}