mengxin_xiao_cai @ 2024-04-05 21:32:39
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n,m;
int a[100010];
int q;
bool isBlue( int num, int x ) {
if( num<x ) {
return true;
}
else{
return false;
}
}
//二分查找
int bin_search( int a[], int x ) {
int l=0,r=n+1;
while(l+1!=r) {
int mid=(l+r)/2;
if(isBlue(a[mid],x)) {
l=mid;
}
else{
r=mid;
}
}
if( a[r]==x ) {
return r;
}
else{
return -1;
}
}
int main()
{
scanf("%d %d",&n,&m);
for( int i=1; i<=n; i++ ) {
scanf("%d",&a[i]);
}
while(m--) {
scanf("%d",&q);
int res=bin_search(a,q);
printf("%d ",res);
}
return 0;
}
by mengxin_xiao_cai @ 2024-04-05 21:37:13
@mengxin_xiao_cai 找到问题了开的数组不够大。。。谢谢大家