코드와이
[BAEKJOON] 2470. 두 용액 본문
문제링크
https://www.acmicpc.net/problem/2470
2470번: 두 용액
첫째 줄에는 전체 용액의 수 N이 입력된다. N은 2 이상 100,000 이하이다. 둘째 줄에는 용액의 특성값을 나타내는 N개의 정수가 빈칸을 사이에 두고 주어진다. 이 수들은 모두 -1,000,000,000 이상 1,000,00
www.acmicpc.net
package acmicpc.Gold5;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class 두_용액 {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int n = Integer.parseInt(br.readLine());
int[] arr = new int[n];
st = new StringTokenizer(br.readLine());
for(int i = 0 ; i < n ; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(arr);
int start = 0;
int end = n - 1;
int ans = Integer.MAX_VALUE;
int result1 = 0;
int result2 = 0;
while(start != end) {
if(ans > Math.abs(arr[start] + arr[end])) {
result1 = arr[start];
result2 = arr[end];
ans = Math.abs(arr[start] + arr[end]);
} else if(arr[start] + arr[end] > 0) {
end--;
} else {
start++;
}
}
System.out.println(result1 + " " + result2);
}
}
'acmicpc' 카테고리의 다른 글
[BAEKJOON] 1033. 감소하는 수 (0) | 2021.07.14 |
---|---|
[BAEKJOON] 14226. 이모티콘 (0) | 2021.07.13 |
[BAEKJOON] 2631. 줄 세우기 (0) | 2021.07.07 |
[BAEKJOON] 11559. Puyo Puyo (0) | 2021.07.06 |
[BAEKJOON] 5557. 1학년 (0) | 2021.07.06 |