-
boj)15655 - N과 M (6)PS/boj 2020. 11. 17. 21:14123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051import java.io.*;import java.util.*;public class boj_16555 {static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));static StringTokenizer st;static int n, m;static int[] a = new int[10];static int[] val;static boolean[] v = new boolean[10];public static void main(String[] args) throws IOException {st = new StringTokenizer(br.readLine());n = Integer.parseInt(st.nextToken());m = Integer.parseInt(st.nextToken());st = new StringTokenizer(br.readLine());val = new int[n];for (int i = 0; i < n; i++) {val[i] = Integer.parseInt(st.nextToken());}Arrays.sort(val);func(0);bw.flush();bw.close();}static void func(int k) throws IOException {if (k == m) {for (int i = 0; i < m; i++) {bw.append(a[i] + " ");}bw.newLine();return;}for (int i = 0; i < n; i++) {if (v[i]) continue;if (k != 0 && a[k - 1] > val[i]) continue;v[i] = true;a[k] = val[i];func(k + 1);v[i] = false;}}}
cs - 백트래킹
- 기존 N과 M 문제와 유사
- 오름차순으로만 조건 변경
'PS > boj' 카테고리의 다른 글
boj)15656 - N과 M (8) (0) 2020.11.18 boj)15656 - N과 M (7) (0) 2020.11.18 boj)15654 - N과 M (5) (0) 2020.11.17 boj)15652 - N과 M (4) (0) 2020.11.17 boj)1654 - 랜선 자르기 (0) 2020.11.17