Skip to content

Commit 78a7eef

Browse files
authored
Merge pull request #114 from AlgorithmWithGod/lkhyun
[20250213] BOJ / 골드4 / 연산자 끼워넣기 / 이강현
2 parents ea425ad + cffc4a7 commit 78a7eef

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
public class Main{
5+
static int N;
6+
static int[] numbers;
7+
static int[] operator = new int[4];
8+
static int max = Integer.MIN_VALUE;
9+
static int min = Integer.MAX_VALUE;
10+
static ArrayDeque<Integer> operation = new ArrayDeque<>();
11+
12+
public static void main(String[] args) throws Exception{
13+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
14+
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
15+
N = Integer.parseInt(br.readLine());
16+
numbers = new int[N];
17+
StringTokenizer st = new StringTokenizer(br.readLine());
18+
for(int i=0;i<N;i++){
19+
numbers[i] = Integer.parseInt(st.nextToken());
20+
}
21+
st = new StringTokenizer(br.readLine());
22+
for(int i=0;i<4;i++){
23+
operator[i] = Integer.parseInt(st.nextToken());
24+
}
25+
findMaxMin(0);
26+
bw.write(max+"\n");
27+
bw.write(min+"\n");
28+
bw.flush();
29+
}
30+
31+
public static void findMaxMin(int depth){
32+
if(depth == N-1){
33+
int total = 0;
34+
int lastElement = numbers[depth];
35+
ArrayDeque<Integer> operandStk = new ArrayDeque<>();
36+
ArrayDeque<Integer> operatorStk = new ArrayDeque<>();
37+
ArrayDeque<Integer> provider = operation.clone();
38+
while(!provider.isEmpty()){
39+
if(!operandStk.isEmpty() && !operatorStk.isEmpty() && operatorStk.getLast()>=2){
40+
int operand = operandStk.pollLast();
41+
int opcode = operatorStk.pollLast();
42+
int result = calculate(operand, provider.pollFirst(), opcode);
43+
operandStk.addLast(result);
44+
operatorStk.addLast(provider.pollFirst());
45+
}else{
46+
operandStk.addLast(provider.pollFirst());
47+
operatorStk.addLast(provider.pollFirst());
48+
}
49+
}
50+
if(!operatorStk.isEmpty() && operatorStk.getLast()>=2){
51+
lastElement = calculate(operandStk.pollLast(), lastElement, operatorStk.pollLast());
52+
}
53+
operandStk.addLast(lastElement);
54+
total = operandStk.pollFirst();
55+
while(!operandStk.isEmpty() && !operatorStk.isEmpty()){
56+
total = calculate(total, operandStk.pollFirst(), operatorStk.pollFirst());
57+
}
58+
max = Math.max(max,total);
59+
min = Math.min(min,total);
60+
return;
61+
}
62+
63+
for(int i=0;i<4;i++){
64+
if(operator[i] > 0){
65+
operation.addLast(numbers[depth]);
66+
operation.addLast(i);
67+
operator[i]--;
68+
findMaxMin(depth+1);
69+
operator[i]++;
70+
operation.pollLast();
71+
operation.pollLast();
72+
}
73+
}
74+
}
75+
public static int calculate(int x, int y, int op){// 0:+ 1:- 2:x 3:/
76+
if(op==0){
77+
return x+y;
78+
}else if(op==1){
79+
return x-y;
80+
}else if(op==2){
81+
return x*y;
82+
}else if(op==3){
83+
return x/y;
84+
}
85+
return 0;
86+
}
87+
}
88+
```

0 commit comments

Comments
 (0)