이전 글에서는 binarySearch를 직접 구현했는데, algorithm 헤더에서 동일한 기능을 하는 binary_search
함수를 제공해준다.
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int N;
scanf("%d", &N);
vector<int> cards;
for (int i = 0; i < N; i++)
{
int number;
scanf("%d", &number);
cards.push_back(number);
}
sort(cards.begin(), cards.end());
int M;
scanf("%d", &M);
for (int i = 0; i < M; i++)
{
int number;
scanf("%d", &number);
if (binary_search(cards.begin(), cards.end(), number))
{
printf("1\n");
}
else
{
printf("0\n");
}
}
return 0;
}
'개발일지 > 알고리즘' 카테고리의 다른 글
백준 10845번 : 큐 (0) | 2025.01.20 |
---|---|
백준 10828번 : 스택 (1) | 2025.01.19 |
백준 10989번 : 수 정렬하기 3 (0) | 2025.01.18 |
백준 2751번 : 수 정렬하기 2 (0) | 2025.01.18 |
백준 2750번: 수 정렬하기 (0) | 2025.01.17 |