알고리즘 문제/BOJ
1920번 수 찾기
parkit
2018. 8. 27. 07:00
728x90
반응형
단순 이분 탐색 문제이다.
https://www.acmicpc.net/problem/1920
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 46 47 48 49 50 51 52 53 54 55 56 | #include <iostream> #include <queue> #include <cstdio> #include <vector> #include <cstring> #include <string> #include <math.h> #include <algorithm> using namespace std; vector<int> A; int N = 0; int search(int left, int right, int search_num) { // left > right = 찾는 숫자가 없다. if (left > right) return 0; // 중간 값 int middle = (left + right) / 2; if (A[middle] == search_num) return 1; // 숫자를 찾았으면 1을 return else if (A[middle] > search_num) search(left, middle - 1, search_num); else search(middle + 1, right, search_num); } int main(void) { int input_num = 0, cnt = 0, search_num = 0; scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d", &input_num); A.push_back(input_num); } // 이분 탐색은 정렬 필요(오름차순) sort(A.begin(), A.end()); scanf("%d", &cnt); while (cnt--) { scanf("%d", &search_num); // not search(A.begin(), A.end(), search_num) printf("%d\n", search(0, A.size() - 1, search_num)); } return 0; } | cs |
728x90
반응형