排列
import java.util.Scanner; public class Main { static int n,m; static int [] a; static void swap(int p, int q){ int x = a[p]; a[p] = a[q]; a[q] = x; } static void dfs(int pos){ if(pos == n){ for(int i = 0; i < n; i++){ System.out.print(a[i] + " "); } System.out.println(); return ; } for(int i= pos; i < n; i++){ swap(pos,i); dfs(pos+1); swap(i,pos); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); n = in.nextInt(); a = new int[n]; for(int i = 0; i < n; i++){ a[i] = in.nextInt(); } dfs(0); } }