package aaa;
import java.util.*;
public class Main {
static Scanner in = new Scanner(System.in);
static boolean[] vis= new boolean[28];
static char[] ans= new char[26];
static char[] c = new char[26];
static int n,cnt;
static void init(){
for (int i = 0; i < c.length; i++) {
c[i]=(char)(97+i);
}
}
static void dfs(int sp){
if(sp==n){
cnt++;
// for (int i = 1; i < n; i++)
// System.out.print(ans[i]+" ");
// System.out.println(ans[n]);
String s = new String(ans);
System.out.println(s);
return;
}
for(int i=0;i< n;i++){
if(!vis[i]){
vis[i]=true;
ans[sp]=c[i];
dfs(sp+1);
vis[i]=false;
}
}
}
public static void main(String[] args) {
init();
while(in.hasNext()){
cnt=0;
Arrays.fill(vis, false);
n = in.nextInt();
dfs(0);
System.out.println(cnt);
}
}
}
简单的dfs过程,数字的全排列更简单。
dfs-字符全排列
最新推荐文章于 2022-04-30 23:17:42 发布