Skip to content

Commit bcf65e5

Browse files
committed
[20260222] BOJ / G3 / 괄호 추가하기 / 김민진
1 parent 11cbbe3 commit bcf65e5

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
```java
2+
import java.io.*;
3+
4+
public class BJ_16637_괄호_추가하기 {
5+
6+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
9+
private static int N, ans;
10+
private static String line;
11+
private static int[] nums;
12+
private static char[] ops;
13+
14+
public static void main(String[] args) throws IOException {
15+
init();
16+
sol();
17+
}
18+
19+
private static void init() throws IOException {
20+
N = Integer.parseInt(br.readLine());
21+
ans = Integer.MIN_VALUE;
22+
23+
int numCnt = N / 2 + 1;
24+
nums = new int[numCnt];
25+
ops = new char[numCnt - 1];
26+
27+
line = br.readLine();
28+
for (int i = 0; i < N; i++) {
29+
if (i % 2 == 0) {
30+
nums[i / 2] = line.charAt(i) - '0';
31+
} else {
32+
ops[i / 2] = line.charAt(i);
33+
}
34+
}
35+
}
36+
37+
private static void sol() throws IOException {
38+
rec(0, nums[0]);
39+
40+
bw.write(ans + "");
41+
bw.flush();
42+
bw.close();
43+
br.close();
44+
}
45+
46+
private static void rec(int idx, int cur) {
47+
if (idx >= ops.length) {
48+
ans = Math.max(ans, cur);
49+
return;
50+
}
51+
52+
int next = nums[idx + 1];
53+
int noBracket = calc(cur, next, ops[idx]);
54+
rec(idx + 1, noBracket);
55+
56+
if (idx + 1 < ops.length) {
57+
int bracketVal = calc(next, nums[idx + 2], ops[idx + 1]);
58+
int withBracket = calc(cur, bracketVal, ops[idx]);
59+
rec(idx + 2, withBracket);
60+
}
61+
}
62+
63+
private static int calc(int a, int b, char op) {
64+
if (op == '+') return a + b;
65+
if (op == '-') return a - b;
66+
return a * b;
67+
}
68+
69+
}
70+
```

0 commit comments

Comments
 (0)