안선생의 개발 블로그

C++ 1920 수 찾기 본문

백준

C++ 1920 수 찾기

안선생 2023. 2. 27. 15:17
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
39
40
41
42
43
44
45
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;
int arr[100001];
int n, m;
void binaryserach(int key)
{
    int start = 0;
    int end = n - 1;
    int mid;
    while (end - start >= 0)
    {
        mid = (end + start)/2;
 
        if (arr[mid] == key)
        {
            cout << 1 << "\n";
            return;
        }
        else if (arr[mid] > key)
            end = mid - 1;
        else
            start =mid+ 1;
    }
    cout << 0 << "\n";
    return;
}
int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> arr[i];
    sort(arr, arr+n);
    cin >> m;
    for (int i = 0; i < m; i++)
    {
        int input;
        cin >> input;
        binaryserach(input);
    }
    return 0;
}
 
cs

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

 

'백준' 카테고리의 다른 글

C++ 24416  (0) 2023.03.08
C++ 2512 예산  (0) 2023.03.01
C++ 5014 스타트링크  (0) 2023.02.26
C++ 14940 쉬운 최단거리  (0) 2023.02.25
C++ 13565 침투  (1) 2023.02.25