반응형
package N과M;

import java.util.*;

public class N과M8 {
	public static boolean visit[];
	public static int arr[];
	public static int num[];
	public static int N, M;
	public static StringBuilder sb;
	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 int[N];
		for (int i = 0; i < N; i++) {
			num[i] = sc.nextInt();
		}
		Arrays.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;
		}

		for (int i = start; i < N; i++) {
			arr[cnt] = num[i];
			permutation(i,cnt + 1);
		}
	}

}
반응형

+ Recent posts