6re神仙代码,求助!

P2249 【深基13.例1】查找

ngmhhy @ 2024-01-10 14:08:15


using namespace std;

int f(int x,int n);
int  a[1000005];
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){
        int mid=(l+r)/2;
        if(a[mid]<x)   l=mid;
        else    r=mid;
    }
    if(a[r]==x)    return r;
    else   return -1;
}

by lyx703 @ 2024-01-10 17:03:39

过了


|