408必学二分查找 #include<iostream> #include<algorithm> using namespace std; int arr1[100]; int arr2[100]; bool binarysearch(int n, int target) //实现二分查找 { int left = 0; int right = n - 1; while(left <= right) { int mid = (left + right) / 2; if (arr1[mid] < target) { left = mid + 1; } else if (target < arr1[mid]) { right = mid - 1; } else return true; //查找成功 } return false; } int main() { int n, m; while (cin >> n) { for (int i = 0; i < n; i++) { cin >> arr1[i]; } cin >> m; for (int i = 0; i < m; i++) { cin >> arr2[i]; } sort(arr1, arr1 + n); //将数组排序 for (int i = 0; i < m; i++) { if (binarysearch(n, arr2[i])) cout << "YES" << endl; else cout << "NO" << endl; } } }