0分求助!!!

P2249 【深基13.例1】查找

zhukexv @ 2024-04-27 17:42:59

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

by AAAuc03 @ 2024-04-27 17:52:03

用格式化


by AAAuc03 @ 2024-04-27 17:54:23

while循环用小于等于


by AAAuc03 @ 2024-04-27 17:56:32

if(a[mid]==b) 还有在while循环里要写等于的情况


by zhukexv @ 2024-04-28 10:53:36

@Zhangenhe03 谢谢


by GGH8866 @ 2024-05-04 20:08:59

@Zhangenhe03 你用一下给我看看!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


|