File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed
Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments