🖥️ CS/Baekjoon Algorithms
#1920번 수 찾기 (C++)
한국의 메타몽
2020. 5. 22. 11:17
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int N,M = 0;
vector<int> n1;
vector<int> m1;
cin >> N;
for(int i=0; i<N; i++){
int a = 0;
cin >> a;
n1.push_back(a);
}
cin >> M;
for(int i=0; i<M; i++){
int b = 0;
cin >> b;
m1.push_back(b);
}
sort(n1.begin(), n1.end()); // 이진탐색 진행을 위한 조건 - 정렬
for(int i=0; i<M; i++){
if(binary_search(n1.begin(),n1.end(),m1[i])) cout << 1 << "\n";
else cout << 0 << "\n";
}
return 0;
}
이진 탐색(Binary Search)를 통해 풀었다.
이진 탐색을 처음 사용했기 때문에, 직접 구현하지는 않고 STL을 가져다 썼다.
다음엔 직접 이진 탐색을 구현해봐야겠다.
* 참고로 이진 탐색의 기본 조건은 '오름/내림 차순으로 정렬이 완료'되야 하기 때문에
탐색을 하기 전 sort로 미리 정렬을 진행했다.