반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
#include <iostream>
#include <algorithm>
#define MAX 100000
using namespace std;
int N,array_N[MAX];
int b_search(int M);
int main(void) {
ios_base::sync_with_stdio(0); cin.tie(0);
int M, n;
cin >> N;
for(int i=0;i<N;i++) {
cin >> array_N[i];
}
sort(array_N, array_N + N);
cin >> M;
for(int i=0;i<M;i++) {
cin >> n;
cout << b_search(n) << "\n"; //endl로 처리시 시간초과
}
}
int b_search(int M) {
int start=0;
int end=N-1;
while(start <= end) {
int mid = (start+end)/2;
if(array_N[mid] == M) return 1;
else if(array_N[mid] < M) start = mid + 1;
else end = mid-1;
}
return 0;
}
|
cs |
반응형
'category > 백준 알고리즘 c++' 카테고리의 다른 글
백준 알고리즘 7576번 토마토 (0) | 2023.02.14 |
---|---|
백준 알고리즘 2447번 별찍기 - 10 (0) | 2023.02.14 |
백준 알고리즘 24479번 알고리즘 수업 - 깊이 우선 탐색 1 (0) | 2023.02.14 |
백준 알고리즘 1002번 터렛 (0) | 2023.02.14 |
백준 알고리즘 4344번 평균은 넘겠지 c++ (0) | 2021.01.20 |
백준 알고리즘 8958번 OX퀴즈 c++ (0) | 2021.01.20 |
백준 알고리즘 1546번 평균 c++ (0) | 2020.12.13 |
백준 알고리즘 3052번 나머지 c++ (0) | 2020.12.13 |
댓글