题解 | #查找#
查找
https://www.nowcoder.com/practice/d93db01c2ee44e8a9237d63842aca8aa
#include <iostream> #include <algorithm> using namespace std; int a[100]; int BS(int target,int n) { int left=0;int right=n-1; while(left<=right) { int middle=left+(right-left)/2; if(target>a[middle])left=middle+1; else if(target<a[middle])right=middle-1; else {return 1;} } return 0; } int main() { int n, m; while (cin >> n) { for(int i=0;i<n;i++) { cin>>a[i]; } cin>>m; int b[m]; for(int i=0;i<m;i++) { cin>>b[i]; } sort(a,a+n); for(int i=0;i<m;i++) { if(BS(b[i],n)==1)cout<<"YES"<<endl; else cout<<"NO"<<endl; } } } // 64 位输出请用 printf("%lld")