题解 | #查找#
查找
https://www.nowcoder.com/practice/d93db01c2ee44e8a9237d63842aca8aa
#include "cstdio" #include "iostream" #include "algorithm" using namespace std; int array[100]; bool BinarySearch(int n, int target) { int left = 0; int right = n - 1; while (left <= right) { int middle = (left + right) / 2; if (array[middle] < target) { left = middle + 1; } else if (target < array[middle]) { right = middle - 1; } else { return true; } } return false; } int main() { int n, m; while (scanf("%d", &n) != EOF) { for (int i = 0; i < n; ++i) { scanf("%d", &array[i]); } sort(array, array + n); scanf("%d", &m); for (int i = 0; i < m; ++i) { int target; scanf("%d", &target); if (BinarySearch(n, target)) { cout << "YES" << endl; } else { cout << "NO" << endl; } } } return 0; }