문제설명
https://www.acmicpc.net/problem/1920
1920번: 수 찾기
첫째 줄에 자연수 N(1 ≤ N ≤ 100,000)이 주어진다. 다음 줄에는 N개의 정수 A[1], A[2], …, A[N]이 주어진다. 다음 줄에는 M(1 ≤ M ≤ 100,000)이 주어진다. 다음 줄에는 M개의 수들이 주어지는데, 이 수들
www.acmicpc.net
코드
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, m, temp;
vector<int> num, finds;
void binarySearch(int key){
int start=0, end=num.size()-1;
while(start <= end){
int mid = (start + end)/2;
if(key == num[mid]){
cout << "1\n";
return;
}
if(key < num[mid]){
end = mid - 1;
}
if(key > num[mid]){
start = mid + 1;
}
}
cout <<"0\n";
return;
}
int main()
{
cin >> n;
for(int i=0; i<n; i++){
cin >> temp;
num.push_back(temp);
}
cin >> m;
for(int i=0; i<m; i++){
cin >> temp;
finds.push_back(temp);
}
sort(num.begin(),num.end());
for(int i=0; i< m; i++)
binarySearch(finds[i]);
return 0;
}
'Algorithm' 카테고리의 다른 글
[백준 3055] 탈출 (1) | 2024.01.31 |
---|---|
[백준 2531] 회전 초밥 (1) | 2024.01.28 |
[프로그래머스] 추억 점수 (0) | 2023.05.12 |
[백준 2468] 안전 영역 (0) | 2023.04.11 |
[프로그래머스] 음양더하기 (0) | 2023.03.05 |