문제: https://www.acmicpc.net/problem/10815
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 | #pragma warning (disable:4996) #include <cstring> #include <climits> #include <algorithm> using namespace std; int binarySearch(const int* numbers, int target, int n) { int low = 0; int high = n - 1; while (low <= high) { int mid = (low + high) / 2; if (numbers[mid] > target) high = mid - 1; else if (target > numbers[mid]) low = mid + 1; else if (numbers[mid] == target) return 1; } return 0; } int main() { int n, m, num; scanf("%d", &n); int* numbers = new int[n]; for (int i = 0; i < n; i++) scanf("%d", &numbers[i]); sort(numbers, numbers + n); scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", &num); printf("%d", binarySearch(numbers, num, n)); if (i != m - 1) printf(" "); } delete[] numbers; return 0; } | cs |
'Algorithm, Data structure > Solved Algorithmic Problem' 카테고리의 다른 글
BAEKJOON 2805 - EKO (0) | 2016.12.28 |
---|---|
BAEKJOON 2869 - PUŽ (0) | 2016.12.26 |
BAEKJOON 1920 - 수 찾기 (0) | 2016.12.25 |
BAEKJOON 2193 - 이친수 (0) | 2016.12.25 |
BAEKJOON 1629 - 곱셈 (0) | 2016.12.24 |