题解 | #查找#
查找
https://www.nowcoder.com/practice/d93db01c2ee44e8a9237d63842aca8aa
#include<cstdio> #include<algorithm> using namespace std; int arr[100];//使用全局数组 bool binarySearch(int n, int x) { int left = 0; int right = n - 1; while (left <= right) { int mid = (left + right) / 2; if (arr[mid] == x) { return true; } else if (arr[mid] > x) { right = mid - 1 ; } else { left = mid + 1 ; } } return false; } int main() { int n, m ; while (scanf("%d", &n) != EOF) { for (int i = 0 ; i < n ; ++ i) { scanf("%d", &arr[i]); } sort(arr, arr + n); scanf("%d", &m); for (int i = 0 ; i < m ; ++i) { int x; scanf("%d", &x); if (binarySearch(n, x) == true) { printf("YES\n"); } else { printf("NO\n"); } } } }