forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5.java
69 lines (59 loc) Β· 1.88 KB
/
5.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.*;
public class Main {
public static int n;
// μ°μ°μ μννκ³ μ νλ μ 리μ€νΈ
public static ArrayList<Integer> arr = new ArrayList<>();
// λνκΈ°, λΉΌκΈ°, κ³±νκΈ°, λλκΈ° μ°μ°μ κ°μ
public static int add, sub, mul, divi;
// μ΅μκ°κ³Ό μ΅λκ° μ΄κΈ°ν
public static int minValue = (int) 1e9;
public static int maxValue = (int) -1e9;
// κΉμ΄ μ°μ νμ (DFS) λ©μλ
public static void dfs(int i, int now) {
// λͺ¨λ μ°μ°μλ₯Ό λ€ μ¬μ©ν κ²½μ°, μ΅μκ°κ³Ό μ΅λκ° μ
λ°μ΄νΈ
if (i == n) {
minValue = Math.min(minValue, now);
maxValue = Math.max(maxValue, now);
}
else {
// κ° μ°μ°μμ λνμ¬ μ¬κ·μ μΌλ‘ μν
if (add > 0) {
add -= 1;
dfs(i + 1, now + arr.get(i));
add += 1;
}
if (sub > 0) {
sub -= 1;
dfs(i + 1, now - arr.get(i));
sub += 1;
}
if (mul > 0) {
mul -= 1;
dfs(i + 1, now * arr.get(i));
mul += 1;
}
if (divi > 0) {
divi -= 1;
dfs(i + 1, now / arr.get(i));
divi += 1;
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 0; i < n; i++) {
int x = sc.nextInt();
arr.add(x);
}
add = sc.nextInt();
sub = sc.nextInt();
mul = sc.nextInt();
divi = sc.nextInt();
// DFS λ©μλ νΈμΆ
dfs(1, arr.get(0));
// μ΅λκ°κ³Ό μ΅μκ° μ°¨λ‘λλ‘ μΆλ ₯
System.out.println(maxValue);
System.out.println(minValue);
}
}