BOJ 3273 두 수의 합

Jmnote (토론 | 기여)님의 2023년 11월 27일 (월) 19:36 판 (→‎C++)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요[ | ]

BOJ 3273 두 수의 합


2 C++[ | ]

#include <bits/stdc++.h>
using namespace std;

int n, x;
int A[100001];

void solve() {
    sort(A, A+n);
    int left=0, right=n-1;
    int answer = 0;
    while(left < right) {
        if(A[left]+A[right] == x) {
            answer++;
            right--;
        } else if (A[left]+A[right] > x) {
            right--;
        } else {
            left++;
        }
    }
    cout << answer;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for(int i=0; i<n; i++) {
        cin >> A[i];
    }
    cin >> x;
    solve();
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}