Skip to content

Commit 10e58c7

Browse files
committed
issue sowon-dev#42 14888
1 parent cfad11b commit 10e58c7

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

src/backjoon/_14888.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package backjoon;
2+
// https://www.acmicpc.net/problem/14888
3+
// 연산자 끼워넣기
4+
5+
import java.io.BufferedReader;
6+
import java.io.IOException;
7+
import java.io.InputStreamReader;
8+
import java.util.StringTokenizer;
9+
10+
public class _14888 {
11+
public static int N; // 주어진 숫자 개수
12+
public static int[] number; // 숫자
13+
public static int[] operator = new int[4]; // 덧셈, 뺄셈, 곱셈, 나눗셈 각각의 개수
14+
public static int MAX = Integer.MIN_VALUE; // 최댓값
15+
public static int MIN = Integer.MAX_VALUE; // 최솟값
16+
17+
public static void main(String[] args) throws IOException {
18+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
19+
// memory 13680 runtime 84
20+
N = Integer.parseInt(br.readLine());
21+
number = new int[N];
22+
23+
// 숫자들 배열에 넣기
24+
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
25+
for (int i = 0; i < N; i++) {
26+
number[i] = Integer.parseInt(st.nextToken());
27+
}
28+
29+
// 연산자 배열에 넣기
30+
st = new StringTokenizer(br.readLine(), " ");
31+
for (int i = 0; i < 4; i++) {
32+
operator[i] = Integer.parseInt(st.nextToken());
33+
}
34+
35+
dfs(number[0], 1);
36+
37+
System.out.println(MAX);
38+
System.out.println(MIN);
39+
40+
}
41+
42+
public static void dfs(int num, int idx) {
43+
if (idx == N) {
44+
MAX = Math.max(MAX, num);
45+
MIN = Math.min(MIN, num);
46+
return;
47+
}
48+
49+
for (int i = 0; i < 4; i++) {
50+
// 연산자 개수가 1개 이상인 경우
51+
if (operator[i] > 0) {
52+
53+
// 해당 연산자를 1 감소시킨다.
54+
operator[i]--;
55+
56+
switch (i) {
57+
58+
case 0:
59+
dfs(num + number[idx], idx + 1);
60+
break;
61+
case 1:
62+
dfs(num - number[idx], idx + 1);
63+
break;
64+
case 2:
65+
dfs(num * number[idx], idx + 1);
66+
break;
67+
case 3:
68+
dfs(num / number[idx], idx + 1);
69+
break;
70+
71+
}
72+
// 재귀호출이 종료되면 다시 해당 연산자 개수를 복구한다.
73+
operator[i]++;
74+
}
75+
}
76+
}
77+
}
78+
/*
79+
INPUT
80+
2
81+
5 6
82+
0 0 1 0
83+
84+
OUTPUT
85+
30
86+
30
87+
*/

0 commit comments

Comments
 (0)