-
Notifications
You must be signed in to change notification settings - Fork 0
/
BJ15663_N๊ณผM9.java
60 lines (57 loc) ยท 1.77 KB
/
BJ15663_N๊ณผM9.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.ssafy.nm;
import java.io.*;
import java.util.*;
/**
* 15663. N๊ณผ M (9)
* N๊ฐ์ ์์ฐ์์ ์์ฐ์ M์ด ์ฃผ์ด์ก์ ๋, ๊ธธ์ด๊ฐ M์ธ ์์ด์ ๋ชจ๋ ๊ตฌํ์์ค.
* ์กฐ๊ฑด : N๊ฐ์ ์์ฐ์ ์ค์์ M๊ฐ๋ฅผ ๊ณ ๋ฅธ ์์ด
* : ์์ด์ ์ฌ์ ์์ผ๋ก ์ฆ๊ฐํ๋ ์์๋ก ์ถ๋ ฅํด์ผ ํ๋ค.
*/
public class BJ15663_N๊ณผM9 {
private static StringBuilder result = new StringBuilder();
private static int N;
private static int M;
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(in.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
st = new StringTokenizer(in.readLine());
int[] input = new int[N];
for (int i = 0; i < N; i++) {
input[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(input);
combination(input, new int[M], new boolean[N], 0);
System.out.println(result);
}
private static void combination(int[] input, int[] output, boolean[] selected, int cnt) {
// ์์ด์ ์์ฑํ๋ฉด ์ถ๋ ฅ
if (cnt == M) {
for (int i = 0; i < M; i++) {
result.append(output[i]).append(" ");
}
result.append("\n");
return;
}
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < N; i++) {
// ์ด๋ฏธ ์ ํ๋ ์ซ์๋ ์ง๋์น๋ค.
if (selected[i]) continue;
// ๊ฐ์ ์๋ฆฌ์ ์ค๋ณต๋ ์ซ์๋ ์ง๋์น๋ค.
boolean isExist = false;
for (int j = 0; j < list.size(); j++) {
if (list.get(j) == input[i]) {
isExist = true;
break;
}
}
if (isExist) continue;
output[cnt] = input[i];
list.add(input[i]);
selected[i] = true;
combination(input, output, selected, cnt+1);
selected[i] = false;
}
}
}