样例最后的-1输不出,悬关

P2249 【深基13.例1】查找

lucy2012 @ 2024-03-18 19:27:04

#include<bits/stdc++.h>
using namespace std;
int n,m,a[1000010],num;
int find(int x){
    int r=n+1,l=1;
    while(l<r){
        int mid=l+(r-1)/2;
        if(a[mid]>=x)
            r=mid;
        else
            l=mid+1;
    }
    if(a[l]==x)
       return l;
    else
       return -1;
}
int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=m;i++){
        cin>>num;
        cout<<find(num)<<' ';
    }
    return 0;
}   

by chenyyegg @ 2024-03-18 19:31:47

int r=n,l=1,mid;
    while(l<r){
        mid=(l+r)/2;

by lucy2012 @ 2024-03-18 19:34:12

@chenyyegg 啊!为神马这么改对了?


|