-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathBJ_10974.java
51 lines (41 loc) · 1.02 KB
/
BJ_10974.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
import java.util.Scanner;
public class BJ_10974 {
// 백준알고리즘 10974번 모든순열
static int N;
static int[] arr;
static boolean[] visited;
static int[] output;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
N = sc.nextInt();
arr = new int[N];
output = new int[N];
visited = new boolean[N];
for (int i = 0; i < N; i++) {
arr[i] = i + 1;
}
perm(arr, output, visited, 0, N, N);
}
// 순서를 지키면서 n 개중에서 r 개를 뽑는 경우
static void perm(int[] arr, int[] output, boolean[] visited, int depth, int n, int r) {
if (depth == r) {
print(output, r);
return;
}
for (int i = 0; i < n; i++) {
if (visited[i] != true) {
visited[i] = true;
output[depth] = arr[i];
perm(arr, output, visited, depth + 1, n, r);
visited[i] = false;
;
}
}
}
static void print(int[] arr, int r) {
for (int i = 0; i < r; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
}