실버 3
https://www.acmicpc.net/problem/15654
15649번 N과 M(1) 비슷하지만, 이 문제는 수열을 입력받아 배열(input)에 저장해서 처리해야한다.
package 백트래킹;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class boj_15653 {
static StringBuilder sb = new StringBuilder();
static int n, m;
static boolean[] visit;
static int[] arr;
static int[] input;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
input = new int[n];
for(int i=0;i<n;i++){
input[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(input);
visit = new boolean[n];
arr = new int[m];
dfs(0);
System.out.println(sb);
}
private static void dfs(int depth) {
if (depth == m) {
for (int val : arr) {
sb.append(val).append(" ");
}
sb.append("\n");
return;
}
for (int i = 0; i < n; i++) {
if (!visit[i]) {
visit[i] = true;
arr[depth] = input[i];
dfs(depth + 1);
visit[i] = false;
}
}
}
}