-
boj)15664 - N과 M (10)PS/boj 2020. 11. 19. 10:3712345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455import java.io.*;import java.util.*;public class boj_15664 {static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));static StringTokenizer st;static boolean[] v = new boolean[10];static int[] a = new int[10];static int[] val;static int n, m;public static void main(String[] args) throws IOException {st = new StringTokenizer(br.readLine());n = Integer.parseInt(st.nextToken());m = Integer.parseInt(st.nextToken());val = new int[n];st = new StringTokenizer(br.readLine());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;}int before = 0;for (int i = 0; i < n; i++) {if (v[i]) continue;if (before == val[i]) continue;if (k != 0 && a[k-1] > val[i]) continue;a[k] = val[i];v[i] = true;before = a[k];func(k + 1);v[i] = false;}}}
cs - 백트래킹
- 중복제거, 비내림차순 정렬
'PS > boj' 카테고리의 다른 글
boj)15666 - N과 M (12) (0) 2020.11.19 boj)15665 - N과 M (11) (0) 2020.11.19 boj)2448 - 별 찍기 - 11 (0) 2020.11.19 boj)15663 - N과 M (9) (0) 2020.11.18 boj)15656 - N과 M (8) (0) 2020.11.18