-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path_14888.java
More file actions
87 lines (72 loc) ยท 2.06 KB
/
_14888.java
File metadata and controls
87 lines (72 loc) ยท 2.06 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
75
76
77
78
79
80
81
82
83
84
85
86
87
package backjoon;
// https://www.acmicpc.net/problem/14888
// ์ฐ์ฐ์ ๋ผ์๋ฃ๊ธฐ
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class _14888 {
public static int N; // ์ฃผ์ด์ง ์ซ์ ๊ฐ์
public static int[] number; // ์ซ์
public static int[] operator = new int[4]; // ๋ง์
, ๋บ์
, ๊ณฑ์
, ๋๋์
๊ฐ๊ฐ์ ๊ฐ์
public static int MAX = Integer.MIN_VALUE; // ์ต๋๊ฐ
public static int MIN = Integer.MAX_VALUE; // ์ต์๊ฐ
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// memory 13680 runtime 84
N = Integer.parseInt(br.readLine());
number = new int[N];
// ์ซ์๋ค ๋ฐฐ์ด์ ๋ฃ๊ธฐ
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < N; i++) {
number[i] = Integer.parseInt(st.nextToken());
}
// ์ฐ์ฐ์ ๋ฐฐ์ด์ ๋ฃ๊ธฐ
st = new StringTokenizer(br.readLine(), " ");
for (int i = 0; i < 4; i++) {
operator[i] = Integer.parseInt(st.nextToken());
}
dfs(number[0], 1);
System.out.println(MAX);
System.out.println(MIN);
}
public static void dfs(int num, int idx) {
if (idx == N) {
MAX = Math.max(MAX, num);
MIN = Math.min(MIN, num);
return;
}
for (int i = 0; i < 4; i++) {
// ์ฐ์ฐ์ ๊ฐ์๊ฐ 1๊ฐ ์ด์์ธ ๊ฒฝ์ฐ
if (operator[i] > 0) {
// ํด๋น ์ฐ์ฐ์๋ฅผ 1 ๊ฐ์์ํจ๋ค.
operator[i]--;
switch (i) {
case 0:
dfs(num + number[idx], idx + 1);
break;
case 1:
dfs(num - number[idx], idx + 1);
break;
case 2:
dfs(num * number[idx], idx + 1);
break;
case 3:
dfs(num / number[idx], idx + 1);
break;
}
// ์ฌ๊ทํธ์ถ์ด ์ข
๋ฃ๋๋ฉด ๋ค์ ํด๋น ์ฐ์ฐ์ ๊ฐ์๋ฅผ ๋ณต๊ตฌํ๋ค.
operator[i]++;
}
}
}
}
/*
INPUT
2
5 6
0 0 1 0
OUTPUT
30
30
*/