-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP15664_순열.java
65 lines (57 loc) · 1.63 KB
/
P15664_순열.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
61
62
63
64
65
package net.acmicpc.순열조합;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
public class P15664_순열 {
static int M;
static int N;
static boolean[] checked;
static int[] numbers;
static int[] result;
static ArrayList<String> list;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] NM = br.readLine().split(" ");
N = Integer.parseInt(NM[0]);
M = Integer.parseInt(NM[1]);
numbers = new int[N];
checked = new boolean[N];
result = new int[M];
list = new ArrayList<>();
String[] num = br.readLine().split(" ");
for (int i = 0; i < N; ++i) {
numbers[i] = Integer.parseInt(num[i]);
}
Arrays.sort(numbers);
permutation(0);
for (String s : list) {
System.out.println(s);
}
}
private static void permutation(int index) {
if (index == M) {
boolean flag = true;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.length - 1; ++i) {
if (result[i] > result[i + 1]) flag = false;
}
if (flag) {
for (int v : result) {
sb.append(v + " ");
}
if (!list.contains(sb.toString())) list.add(sb.toString());
}
return;
}
for (int i = 0; i < N; ++i) {
if (!checked[i]) {
result[index] = numbers[i];
checked[i] = true;
permutation(index + 1);
checked[i] = false;
}
}
}
}