백준 10815번: 숫자카드 [c++]

gogowonji·2022년 3월 27일
0

알고리즘

목록 보기
4/7

백준 10815번: 숫자카드

//  Created by gogowonji on 2022/03/27.
//

#include <iostream>
#include <algorithm>
using namespace std;


int input[500001];

bool binary_search(int n, int target){
    int start = 0;
    int end = n - 1;
    while (start <= end)
    {
        int mid = (start + end)/2;
        
        if(input[mid] == target){
            return 1;
        }else if(input[mid] > target){
            end = mid - 1;
        }else{
            start = mid + 1;
        }
    }
    return 0;
}


int main() {
    // insert code here...
    // 백준 10815번 숫자 카드
    /*
     숫자 카드는 정수 하나가 적혀져 있는 카드이다. 상근이는 숫자 카드 N개를 가지고 있다. 정수 M개가 주어졌을 때, 이 수가 적혀있는 숫자 카드를 상근이가 가지고 있는지 아닌지를 구하는 프로그램
     */
    
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
        
    int n,m;
    
    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> input[i];
    }
   
    sort(input, input+n);

    cin >> m;
    
    for(int j =0; j < m; j++){
        int k;
        cin >> k;
        cout << binary_search(n,k) << " ";
    }
    
    
    
    
    return 0;
}

ios_base::sync_with_stdio(0);
cin.tie(NULL);
입출력 때문에 시간초과 뜸
-> 참고

중요
정렬을 해주고 이진탐색을 해줘야함
sort(input, input+n);

오랜만에 이진탐색을 하니까
정렬도 빼먹고 출력 하는 것도 어떻게 해줄 지 고민했던 것 같다.
감을 잃지 않도록 꾸준히 풀어야 겠다.

profile
개발자를 할까 말까

0개의 댓글