-
Notifications
You must be signed in to change notification settings - Fork 0
/
210129_15663_N과M(9)_s2.java
49 lines (42 loc) · 1.4 KB
/
210129_15663_N과M(9)_s2.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
import java.io.*;
import java.util.*;
public class Main {
static int N, M;
static int numbers[];
static int visited[];
static LinkedHashSet<String> set = new LinkedHashSet<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
numbers = new int[N];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; ++i) {
numbers[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(numbers);
visited = new int[N];
for (int i = 0; i < N; ++i) {
visited[i] = 1;
backTracking(i, 1, Integer.toString(numbers[i])+" ");
visited[i] = 0;
}
for (String s: set) {
System.out.println(s);
}
}
static void backTracking(int idx, int cnt, String str) {
if (cnt == M) {
set.add(str);
return;
}
for (int i = 0; i < N; ++i) {
if (visited[i] == 0) {
visited[i] = 1;
backTracking(i, cnt+1, str+Integer.toString(numbers[i])+" ");
visited[i] = 0;
}
}
}
}