본문 바로가기
백준

[1920번] 수 찾기

by 2744m 2019. 3. 11.

/*1920번 수 찾기*/
#include <iostream>
#include <algorithm>
using namespace std;
int N, M;
int A[100000],B;

int search(int target) {
    int front = 0;
    int rear = N - 1;
    int mid;
    while (front <= rear) {
        mid = (front + rear) / 2;
        if (A[mid] == target) return 1;
        else {
            if (A[mid] < target) {
                front = mid + 1;
            }
            else{
                rear = mid - 1;
            }
        }
    }
    return 0;
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }
    sort(A, A + N);
    cin >> M;
    for (int i = 0; i < M; i++){
        cin >> B;
        if (search(B) == 0)
            cout << 0 << '\n';
        else
            cout << 1 << '\n';
    }
}

이분탐색을 이용한 풀이

정렬은 STL을 사용하였다.

'백준' 카테고리의 다른 글

[10989]수 정렬하기 3  (0) 2019.03.11
[2750,2751번] 수 정렬하기  (0) 2019.03.11
[5014번] 스타트링크  (0) 2019.02.12
[3184번] 양  (0) 2019.02.11
[15683번] 감시  (0) 2019.01.23

댓글