题解 | #查找#
查找
https://www.nowcoder.com/practice/d93db01c2ee44e8a9237d63842aca8aa
#include <iostream> #include <algorithm> using namespace std; const int N = 101; int a[N], b[N]; bool find(int a[], int n, int x){ int l = 0, r = n - 1; while (l <= r) { int mid = l + r >> 1; if (a[mid] < x) { l = mid + 1; }else if (a[mid] > x) { r = mid - 1; }else { return true; } } return false; } int main() { int n, m; while (cin >> n) { for(int i = 0; i < n; i ++) cin >> a[i]; cin >> m; for(int i =0; i < m; i ++) cin >> b[i]; sort(a, a + n); for(int i = 0; i < m; i ++){ bool res = find(a, n, b[i]); if(res) cout << "YES" << endl; else cout << "NO" << endl; } } return 0; } // 64 位输出请用 printf("%lld")