求助大佬,不知道哪里有问题
查看原帖
求助大佬,不知道哪里有问题
871424
lnqlnq666楼主2022/12/10 09:27
#include<bits/stdc++.h>
#include<stdio.h>
using namespace std;
int mysearch(int x);
long long r,n,m,a[10000001],q;
int main(){
    scanf("%lld %lld",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
    }
    for(int j=1;j<=m;j++){
        scanf("%lld",&q);
        r=mysearch(q);
        printf("%lld ",r);
    }
    return 0;
}
int mysearch(int x){
    int mid,low,high;
    low=1;high=n;
    while(low<=high){
        mid=(high+low)/2;
        if(a[mid]==x)return mid;
        else if(a[mid]<x)  low=mid+1;
        else if(a[mid]>x) high=mid-1;
    }
    return -1;
}
2022/12/10 09:27
加载中...