Skip to content

Commit 9c59c01

Browse files
committed
solve: 프로그래머스 문제 풀이 - 11일차
1 parent c711e3e commit 9c59c01

File tree

3 files changed

+122
-0
lines changed

3 files changed

+122
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.io.*;
2+
3+
public class Main {
4+
static int N, M;
5+
// static boolean[] visited;
6+
static int[] selected;
7+
8+
public static void main(String[] args) throws IOException {
9+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
10+
11+
String[] parts = br.readLine().split(" ");
12+
13+
N = Integer.parseInt(parts[0]);
14+
M = Integer.parseInt(parts[1]);
15+
16+
// visited = new boolean[N + 1];
17+
selected = new int[M];
18+
dfs(1, 0);
19+
}
20+
public static void dfs(int curr, int depth) {
21+
if (depth == M) {
22+
for (int num: selected) {
23+
System.out.print(num + " ");
24+
}
25+
System.out.println();
26+
return;
27+
}
28+
29+
for (int i=curr; i<=N; i++) {
30+
selected[depth] = i;
31+
dfs(i + 1, depth + 1);
32+
33+
// 항상 i보다 큰 숫자만 탐색하므로 visited가 필요가 없다
34+
/*
35+
if (!visited[i]) {
36+
visited[i] = true;
37+
selected[depth] = i;
38+
dfs(i + 1, depth + 1);
39+
visited[i] = false;
40+
}
41+
*/
42+
}
43+
}
44+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.io.*;
2+
3+
public class Main {
4+
static int N, M;
5+
static int[] selected;
6+
7+
public static void main(String[] args) throws IOException {
8+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
10+
String[] parts = br.readLine().split(" ");
11+
12+
N = Integer.parseInt(parts[0]);
13+
M = Integer.parseInt(parts[1]);
14+
15+
selected = new int[M];
16+
dfs(1, 0);
17+
}
18+
public static void dfs(int curr, int depth) {
19+
if (depth == M) {
20+
for (int num: selected) {
21+
System.out.print(num + " ");
22+
}
23+
System.out.println();
24+
return;
25+
}
26+
for (int i=curr; i<=N; i++) {
27+
selected[depth] = i;
28+
dfs(i, depth + 1);
29+
}
30+
}
31+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
static int N, M;
6+
static int[] inputs;
7+
static boolean[] visited;
8+
static int[] result;
9+
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
13+
String[] parts = br.readLine().split(" ");
14+
15+
N = Integer.parseInt(parts[0]);
16+
M = Integer.parseInt(parts[1]);
17+
18+
inputs = new int[N];
19+
visited = new boolean[N];
20+
result = new int[M];
21+
String[] inputParts = br.readLine().split(" ");
22+
for (int i=0; i<N; i++) {
23+
inputs[i] = Integer.parseInt(inputParts[i]);
24+
}
25+
26+
Arrays.sort(inputs);
27+
dfs(0, 0);
28+
}
29+
public static void dfs(int curr, int depth) {
30+
if (depth == M) {
31+
for (int num: result) {
32+
System.out.print(num + " ");
33+
}
34+
System.out.println();
35+
return;
36+
}
37+
38+
for (int i=0; i<N; i++) {
39+
if (!visited[i]) {
40+
visited[i] = true;
41+
result[depth] = inputs[i];
42+
dfs(i, depth + 1);
43+
visited[i] = false;
44+
}
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)