-
Notifications
You must be signed in to change notification settings - Fork 0
/
연산자 끼워넣기.java
54 lines (45 loc) · 1.66 KB
/
연산자 끼워넣기.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.io.*;
import java.util.*;
public class Main {
static int[] arr;
static int N;
static int[] op;
static int answerMax = Integer.MIN_VALUE;
static int answerMin = Integer.MAX_VALUE;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
arr = new int[N];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
op = new int[4];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < 4; i++) {
op[i] = Integer.parseInt(st.nextToken());
}
solve(1, arr[0]);
System.out.println(answerMax);
System.out.println(answerMin);
}
static void solve(int depth, int result) {
if (depth == N) {
answerMax = Math.max(answerMax, result);
answerMin = Math.min(answerMin, result);
return;
}
for (int i = 0; i < 4; i++) {
if (op[i] > 0) {
op[i]--;
switch (i) {
case 0: solve(depth + 1, result + arr[depth]); break;
case 1: solve(depth + 1, result - arr[depth]); break;
case 2: solve(depth + 1, result * arr[depth]); break;
case 3: solve(depth + 1, result / arr[depth]); break;
}
op[i]++;
}
}
}
}