반응형

2470

https://www.acmicpc.net/problem/2470

 

 

[정답]

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
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
 
#include <iostream>
#include <algorithm>
using namespace std;
 
#define ABS(a) ((a) <0-(a):(a))
 
int n;
int s, e;
int a[100001= { 0, };
int res = (int)21e8;
int ansA, ansB;
 
void input() {
    scanf("%d"&n);
    s = 0;
    e = n - 1;
    for (int i = 0; i < n; i++scanf("%d", a + i);
    sort(a, a + n);
}
 
int main(void) {
    int temp;
    input();
    while (s < e) {
        temp = a[s] + a[e];
        if (ABS(temp) < res) {
            res = ABS(temp);
            ansA = a[s];
            ansB = a[e];
        }
        if (res == 0break;
        else if (temp < 0) s++;
        else e--;
    }
    cout << ansA << " " << ansB;
    return 0;
}
cs

.

.

.

[풀이]

투포인터 문제다.

 

p.s.

오랜만에 글을 올린다.

백준 문제풀이는 계속 해왔고, 이전에 푼 문제들을 꾸준히 올려보겠다.

C++ 전문이 되어버렸다만..

반응형

+ Recent posts