ngmhhy @ 2024-01-30 17:08:22
#include<bits/stdc++.h>
using namespace std;
int f(int x,int n);
int a[100005],mid;
int main(){
int n,t,num;
scanf("%d%d",&n,&t);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
}
for(int i=1;i<=t;i++){
scanf("%d",&num);
printf("%d ",f(num,n));
}
}
int f(int x,int n){
int l=0,r=n+1;
while(l+1!=r){
mid=(l+r)/2;
if(a[mid]<x) l=mid;
else r=mid;
}
if(a[r]==x) return r;
else return -1;
}
by PRew_ @ 2024-01-30 17:18:48
试试把数组开大点,可能边界写错了
by PRew_ @ 2024-01-30 17:19:05
@ngmhhy
by ngmhhy @ 2024-01-30 17:22:04
是我数组开小了,谢谢提醒
by ngmhhy @ 2024-01-30 17:22:52
@nothing_exe_studio
by PRew_ @ 2024-01-30 17:32:33
不用QWQ