过样例了,但爆零了,感觉代码没问题呀,求大佬指点

P2249 【深基13.例1】查找

拱垲 @ 2023-09-28 10:05:02

#include <iostream>
#include <cstdio>
#include<algorithm>
#include<string>
#include<map>
using namespace std;

int num[100005];
int n,m,q;

int search(int l,int r)
{
    if(l==r)
        if(num[l]==q)
            return l;
        else
            return -1;
    int m=(l+r)/2;
    if(num[m]>=q)return search(l,m);
    else return search(m+1,r);
}

int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        scanf("%d",&num[i]);
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&q);
        printf("%d ",search(1,n));
    }
    return 0;
}

by zhangbo1000 @ 2023-09-28 10:37:29

n<=1e6....


by Holmes_Wang @ 2023-09-28 10:55:11

数组开小了,多加个0就A了


by 拱垲 @ 2023-10-17 16:50:50

@wangchunjie 哇,谢谢大佬。抱歉才看到消息


by 拱垲 @ 2023-10-17 16:51:06

@zhangbo1000 谢谢大佬。抱歉才看到消息


|