题解 | #查找#
查找
http://www.nowcoder.com/practice/d93db01c2ee44e8a9237d63842aca8aa
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
std::ios::sync_with_stdio(false);
int n,m,left,right,temp;
while(cin >> n){
int *num = new int[n];
for(int i = 0;i < n;++i)
cin >> num[i];
cin >> m;
for(int i = 0;i < m;++i){
cin >> temp;
sort(num,num + n);
left = 0,right = n -1;
while(left <= right){
int mid = (left + right) / 2;
if(num[mid] == temp){
cout << "YES" << endl;
break;
}
else if(num[mid] < temp)
left = mid + 1;
else
right = mid - 1;
}
if(right < left)
cout << "NO" << endl;
}
}
}