1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| import java.util.*; import java.io.*; public class Main{ static List<List<Integer>> res = new ArrayList<>(); static Set<Integer> set = new HashSet<>(); static BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out)); public static void main(String[] args) throws Exception{ Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = n; dfs(1, n, m, new ArrayList<>()); for(List<Integer> list : res){ for(int i = 0; i < list.size(); i++){ log.write(String.valueOf(list.get(i))); if(i != list.size()-1){ log.write(" "); } } log.write("\n"); } log.flush(); } public static void dfs(int cur, int n, int m, List<Integer> ans){ if(ans.size() == m){ res.add(new ArrayList<>(ans)); return; } for(int i = cur; i <= n; i++){ if(set.contains(i)){ continue; } ans.add(i); set.add(i); dfs(cur, n, m, ans); ans.remove(ans.size()-1); set.remove(i); } } }
|