Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 10e58c7

Browse files
issue #42 14888
1 parent cfad11b commit 10e58c7

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

β€Žsrc/backjoon/_14888.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package backjoon;
2+
// https://www.acmicpc.net/problem/14888
3+
// μ—°μ‚°μž λΌμ›Œλ„£κΈ°
4+
5+
import java.io.BufferedReader;
6+
import java.io.IOException;
7+
import java.io.InputStreamReader;
8+
import java.util.StringTokenizer;
9+
10+
public class _14888 {
11+
public static int N; // μ£Όμ–΄μ§„ 숫자 개수
12+
public static int[] number; // 숫자
13+
public static int[] operator = new int[4]; // λ§μ…ˆ, λΊ„μ…ˆ, κ³±μ…ˆ, λ‚˜λˆ—μ…ˆ 각각의 개수
14+
public static int MAX = Integer.MIN_VALUE; // μ΅œλŒ“κ°’
15+
public static int MIN = Integer.MAX_VALUE; // μ΅œμ†Ÿκ°’
16+
17+
public static void main(String[] args) throws IOException {
18+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
19+
// memory 13680 runtime 84
20+
N = Integer.parseInt(br.readLine());
21+
number = new int[N];
22+
23+
// μˆ«μžλ“€ 배열에 λ„£κΈ°
24+
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
25+
for (int i = 0; i < N; i++) {
26+
number[i] = Integer.parseInt(st.nextToken());
27+
}
28+
29+
// μ—°μ‚°μž 배열에 λ„£κΈ°
30+
st = new StringTokenizer(br.readLine(), " ");
31+
for (int i = 0; i < 4; i++) {
32+
operator[i] = Integer.parseInt(st.nextToken());
33+
}
34+
35+
dfs(number[0], 1);
36+
37+
System.out.println(MAX);
38+
System.out.println(MIN);
39+
40+
}
41+
42+
public static void dfs(int num, int idx) {
43+
if (idx == N) {
44+
MAX = Math.max(MAX, num);
45+
MIN = Math.min(MIN, num);
46+
return;
47+
}
48+
49+
for (int i = 0; i < 4; i++) {
50+
// μ—°μ‚°μž κ°œμˆ˜κ°€ 1개 이상인 경우
51+
if (operator[i] > 0) {
52+
53+
// ν•΄λ‹Ή μ—°μ‚°μžλ₯Ό 1 κ°μ†Œμ‹œν‚¨λ‹€.
54+
operator[i]--;
55+
56+
switch (i) {
57+
58+
case 0:
59+
dfs(num + number[idx], idx + 1);
60+
break;
61+
case 1:
62+
dfs(num - number[idx], idx + 1);
63+
break;
64+
case 2:
65+
dfs(num * number[idx], idx + 1);
66+
break;
67+
case 3:
68+
dfs(num / number[idx], idx + 1);
69+
break;
70+
71+
}
72+
// μž¬κ·€ν˜ΈμΆœμ΄ μ’…λ£Œλ˜λ©΄ λ‹€μ‹œ ν•΄λ‹Ή μ—°μ‚°μž 개수λ₯Ό λ³΅κ΅¬ν•œλ‹€.
73+
operator[i]++;
74+
}
75+
}
76+
}
77+
}
78+
/*
79+
INPUT
80+
2
81+
5 6
82+
0 0 1 0
83+
84+
OUTPUT
85+
30
86+
30
87+
*/

0 commit comments

Comments
(0)

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /