32分蒟蒻求助,其他全RE,有没有什么需要改的地方

P2249 【深基13.例1】查找

jijijibeibeibei @ 2023-09-07 18:51:33

#include<iostream>//p2249查找 ,运行错误 
#include<cstring>
using namespace std;
int a[1000005],b[10005];//a待查询,b要查询 
int main(){
     ios::sync_with_stdio(0);
    int n,m,mun;
    cin>>n>>m;//数字个数和询问次数 
    int c=1,d=n;//mun表中点,c是起点,d是终点,b[i]在c,d之间 
    for(int i=1;i<=n;i++){
        cin>>a[i];//待查询的数字 
    }
    for(int i=1;i<=m;i++){
        cin>>b[i];//需要查询的数字 
        c = 1; d = n;
        while(c<d){
            mun = c + (d-c) / 2;
            if( a[mun] < b[i] ) c=mun+1 ;//比中点大,找后半段 
            else d=mun;
        }
        if(a[c]==b[i])cout<<c<<" ";
        else cout<<-1<<" ";
    }
    return 0;
}

by jijijibeibeibei @ 2023-09-07 18:54:02

恳请大牛指出什么情况下会运行错误QWQ


by wannacry_ @ 2023-09-07 19:04:36

@jijijibeibeibei b数组开小了


by lirenzehao @ 2023-09-07 19:20:18

b数组太小,爆内存了


by jijijibeibeibei @ 2023-09-08 18:07:54

@wannacry_ 解决了,非常感谢


by jijijibeibeibei @ 2023-09-08 18:08:36

@lirenzehao 已经解决了,谢谢提醒


|