This repository was archived by the owner on Dec 20, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBOJ14888.java
More file actions
74 lines (61 loc) · 1.96 KB
/
Copy pathBOJ14888.java
File metadata and controls
74 lines (61 loc) · 1.96 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
71
72
73
74
import java.util.Scanner;
public class BOJ14888 {
static int n, max = Integer.MIN_VALUE, min = Integer.MAX_VALUE;
static boolean[] visited;
static int[] opOrder;
static int[] number;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
number = new int[n];
for (int i = 0; i < n; i++) {
number[i] = sc.nextInt();
}
opOrder = new int[n - 1];
visited = new boolean[n - 1];
int index = 0;
for (int i = 0; i < 4; i++) {
int cnt = sc.nextInt();
for (int j = 0; j < cnt; j++) {
opOrder[index++] = i + 1;
}
}
dfs(0, 1, number[0], 0);
System.out.println(max);
System.out.println(min);
}
private static void dfs(int v, int index, int num, int len) {
int result = 0;
if (len == n - 1) {
if (num > max) {
max = num;
}
if (num < min) {
min = num;
}
}
else {
for (int i = 0; i < n - 1; i++) {
if (!visited[i]) {
switch (opOrder[i]) {
case 1:
result = num + number[index];
break;
case 2:
result = num - number[index];
break;
case 3:
result = num * number[index];
break;
case 4:
result = num / number[index];
break;
}
visited[i] = true;
dfs(i, index + 1, result, len + 1);
}
}
}
visited[v] = false;
}
}