반응형
package N과M;

import java.util.*;

public class N과M12 {
	public static boolean visit[];
	public static int arr[];
	public static ArrayList<Integer> num;
	public static int N, M;
	public static StringBuilder sb;
	public static String s2;
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = "";
		sb = new StringBuilder(s);
		N = sc.nextInt();
		M = sc.nextInt();
		
		visit = new boolean[N];
		arr = new int[M];
		num = new ArrayList<>();
		
		for (int i = 0; i < N; i++) {
			int n = sc.nextInt();
			num.add(n);
		}
		Collections.sort(num);
		
		permutation(0, 0);
		
		System.out.println(sb);
	}

	public static void permutation(int start , int cnt) {
		if (cnt == M) {
			for (int i = 0; i < arr.length; i++) {
				sb.append(arr[i] + " ");
			}
			sb.append("\n");
			return;
		}

		boolean used[]= new boolean[10001];
		for (int i = start; i < num.size(); i++) {
				if(!used[num.get(i)]) {
					used[num.get(i)] = true;
					arr[cnt] = num.get(i);
					permutation(i, cnt + 1);
					
				}
		}
	}

}
반응형

+ Recent posts