Bj_s2
title: 2026-01-24 author: 강병호 (이름) date: 2026-01-24 (날짜) category: TIL/강병호/2026/01 (파일 경로 : TIL/{이름}/{연}/{월}) layout: post (자유) —
import java.util.*;
import java.io.*;
public class Q15657 {
static Set<String> data = new LinkedHashSet<>();
public static void solve(int[] arr, int[] output, int N, int M, int depth, int index) {
if (index == N && depth < M) {
return;
}
if (depth == M) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < M; i++) {
sb.append(output[i] + " ");
}
data.add(sb.toString());
}
if (depth < M) {
output[depth] = arr[index];
solve(arr, output, N, M, depth + 1, index);
solve(arr, output, N, M, depth + 1, index+1);
solve(arr, output, N, M, depth, index+1);
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
int M = Integer.parseInt(st.nextToken());
int[] arr = new int[N];
int[] output = new int[M];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(arr);
solve(arr, output, N, M, 0, 0);
for (String s : data) {
System.out.println(s);
}
}
}