백준 알고리즘(C++)
백준 3273번 두 수의 합 ( C++ )
coding232624
2024. 9. 3. 21:18
문제
https://www.acmicpc.net/problem/3273
해설
투포인터를 사용하는 간단한 문제
코드
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
|
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int n, x;
long long ret;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n;
vector<int> v(n);
for (int i = 0; i < n; i++)
cin >> v[i];
cin >> x;
sort(v.begin(), v.end());
int start = 0, last = n - 1;
while (start < last)
{
if (v[start] + v[last] == x)
{
ret++;
start++;
last--;
}
else if (v[start] + v[last] > x)
{
last--;
}
else
{
start++;
}
}
cout << ret;
}
|
cs |