提交时间:2020-11-18 18:55:49

运行 ID: 9756

#include<bits/stdc++.h> using namespace std; const int MAXN=100001; int n,m,a[MAXN],num; int binarySearch(int num,int b,int e){ if(b>e) return -1; int index=(b+e)/2; if(num>a[index]){ b=index+1; binarySearch(num,b,e); }else if(num<a[index]){ e=index-1; binarySearch(num,b,e); }else{ return index; } } int main(){ cin>>n>>m; for(int i=0;i<n;i++) cin>>a[i]; for(int i=1;i<=m;i++){ cin>>num; cout<<binarySearch(num,0,n-1)<<endl; } return 0; }