-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP15654_순열.java
50 lines (44 loc) · 1.24 KB
/
P15654_순열.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
package net.acmicpc.순열조합;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class P15654_순열 {
static int M;
static int N;
static boolean[] checked;
static int[] numbers;
static int[] result;
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];
String[] num = br.readLine().split(" ");
for (int i = 0; i < N; ++i) {
numbers[i] = Integer.parseInt(num[i]);
}
Arrays.sort(numbers);
permutation(0);
}
private static void permutation(int index) {
if (index == M) {
for (int i = 0; i < result.length; ++i) {
System.out.print(result[i] + " ");
}
System.out.println();
return;
}
for (int i = 0; i < N; ++i) {
if (!checked[i]) {
result[index] = numbers[i];
checked[i] = true;
permutation(index + 1);
checked[i] = false;
}
}
}
}