Skip to content

Commit f9bae84

Browse files
committed
[20250919] BOJ / G5 / 두 용액 / 김민진
1 parent 1535e0c commit f9bae84

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
```java
2+
import java.io.*;
3+
import java.util.Arrays;
4+
import java.util.StringTokenizer;
5+
6+
public class BJ_2470_두_용액 {
7+
8+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
9+
private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
10+
private static StringTokenizer st;
11+
12+
private static int N;
13+
private static int tmp;
14+
private static int[] ans;
15+
private static int[] solution;
16+
17+
public static void main(String[] args) throws IOException {
18+
init();
19+
sol();
20+
}
21+
22+
private static void init() throws IOException {
23+
N = Integer.parseInt(br.readLine());
24+
tmp = Integer.MAX_VALUE;
25+
26+
ans = new int[2];
27+
solution = new int[N];
28+
st = new StringTokenizer(br.readLine());
29+
for (int i = 0; i < N; i++) {
30+
solution[i] = Integer.parseInt(st.nextToken());
31+
}
32+
Arrays.sort(solution);
33+
}
34+
35+
private static void sol() throws IOException {
36+
int left = 0, right = N - 1;
37+
38+
while (left < right) {
39+
int sum = solution[left] + solution[right];
40+
41+
if (Math.abs(sum) <= tmp) {
42+
ans[0] = solution[left];
43+
ans[1] = solution[right];
44+
45+
tmp = Math.abs(sum);
46+
47+
if (sum == 0) {
48+
break;
49+
}
50+
}
51+
52+
if (sum < 0) {
53+
left++;
54+
} else {
55+
right--;
56+
}
57+
}
58+
bw.write(ans[0] + " " + ans[1]);
59+
bw.flush();
60+
bw.close();
61+
br.close();
62+
}
63+
64+
}
65+
```

0 commit comments

Comments
 (0)