枚举
作者:
goldstine
,
2022-03-31 12:46:51
,
所有人可见
,
阅读 123
排列型枚举
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
static int nn;
static int[] order;
static boolean[] vis;
public static void main(String[] args) throws IOException {
//通过递归实现排列型枚举
BufferedReader cin=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(cin.readLine());
nn=n;
order=new int[n+1];
vis=new boolean[n+1];
dfs(1);
}
public static void dfs(int cur){
if (cur == nn + 1) {
for(int i=1;i<=nn;i++){
System.out.print(order[i]+" ");
}
System.out.println();
}
for(int i=1;i<=nn;i++){
if(vis[i])continue;
order[cur]=i;
vis[i]=true;
dfs(cur+1);
order[cur]=0;
vis[i]=false;
}
}
}