1到n的全排列

import java.util.Scanner;

public class Main{
    static int a[] = new int[10010];
    static boolean [] vis = new boolean[10010];
    static int n = 0;
    public static void print() {
        for(int i = 1; i <= n; i++) { 
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }
    public static void dfs(int x) {
        if(x > n) {
            print();
        }
        for(int i = 1; i<= n; i++) {
            if(!vis[i]) {
                vis[i] = true;
                a[x] = i;
                dfs(x + 1);
                vis[i] = false;
            }
        }
    }
    public static void main(String[]args) {
        Scanner sc = new Scanner(System.in);//
        n = sc.nextInt();
        dfs(1);
    }
}

您可能还喜欢...