Repository navigation
Expand file tree
/
Copy pathBOJ_14888.java
More file actions
70 lines (54 loc) · 1.78 KB
/
Copy pathBOJ_14888.java
File metadata and controls
70 lines (54 loc) · 1.78 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package baekjoon.silver;
import java.io.*;
import java.util.StringTokenizer;
public class BOJ_14888 {
static int N;
static int[] arr; // 수
static int[] op; // 연산자
static int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
static int result;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
arr = new int[st.countTokens()];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
result = arr[0];
st = new StringTokenizer(br.readLine());
op = new int[4];
for (int i = 0; i < op.length; i++) {
op[i] = Integer.parseInt(st.nextToken());
}
dfs(arr[0], 1);
System.out.println(max + "\n" + min);
}
private static void dfs(int result, int depth) {
if(depth == N) {
if(result < min)
min = result;
if(max < result)
max = result;
return;
}
for (int i = 0; i < 4; i++) {
if(op[i] > 0) {
op[i]--;
if(i == 0) { // 덧셈
dfs(result + arr[depth], depth + 1);
}
else if(i == 1) { // 뺄셈
dfs(result - arr[depth], depth + 1);
}
else if(i == 2) { // 곱셈
dfs(result * arr[depth], depth + 1);
}
else { // 나눗셈
dfs(result / arr[depth], depth + 1);
}
op[i]++;
}
}
}
}