Skip to content

Commit 914ecf3

Browse files
authored
Merge pull request #986 from AlgorithmWithGod/lkhyun
[20250927] BOJ / G3 / 괄호 추가하기 / 이강현
2 parents 0fb7a91 + 02799a7 commit 914ecf3

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
public class Main{
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+
static StringTokenizer st;
9+
static StringBuilder sb = new StringBuilder();
10+
static int N;
11+
static String line;
12+
static int max = Integer.MIN_VALUE;
13+
14+
public static void main(String[] args) throws Exception {
15+
N = Integer.parseInt(br.readLine());
16+
line = br.readLine();
17+
18+
dfs(0, Integer.parseInt(String.valueOf(line.charAt(0))));
19+
bw.write(max+"");
20+
bw.close();
21+
}
22+
static void dfs(int idx, int cur) {
23+
24+
if (idx >= N - 1) {
25+
max = Math.max(max, cur);
26+
return;
27+
}
28+
29+
char operator = line.charAt(idx + 1);
30+
int nextNum = Integer.parseInt(String.valueOf(line.charAt(idx + 2)));
31+
32+
int noBracket = calculate(cur, operator, nextNum);
33+
dfs(idx + 2, noBracket);
34+
35+
if (idx + 4 < N) {
36+
char nextOperator = line.charAt(idx + 3);
37+
int nextNextNum = Integer.parseInt(String.valueOf(line.charAt(idx + 4)));
38+
39+
int bracketResult = calculate(nextNum, nextOperator, nextNextNum);
40+
int Bracket = calculate(cur, operator, bracketResult);
41+
dfs(idx + 4, Bracket);
42+
}
43+
}
44+
static int calculate(int a, char op, int b) {
45+
if(op == '+'){
46+
return a+b;
47+
}else if(op == '-'){
48+
return a-b;
49+
}else if(op == '*'){
50+
return a*b;
51+
}else{
52+
return 0;
53+
}
54+
}
55+
}
56+
```

0 commit comments

Comments
 (0)