RE

P2249 【深基13.例1】查找

zcy30 @ 2024-08-19 21:48:58

#include<iostream>
using namespace std;
int a[1000005];
int n,m;
int find(int x)
{
    int l=1,r=n;
    while(l<r)
    {
        int mid=l+(r-l)/2;
        if(a[mid]>=x)r=mid;
        else l=mid+1;

    }
    if(a[l]==x)return l;
    else return -1;
}
int main()
{
    scanf("%d%d",n,m);
    for(int i=1;i<=n;i++)
    scanf("%d",a[i]); int x;
    for(int i=1;i<=m;i++)
    {

        scanf("%d",x);
       int sum=find(x);
       printf("%d",sum);

    }
    return 0;
}

by dongzirui0817 @ 2024-08-19 21:58:47

@zcy30 注意你的读入


by zcy30 @ 2024-08-19 22:02:22

@dongzirui0817 哦,谢谢大佬


by Yxy7952 @ 2024-08-28 13:53:02

@dongzirui0817

请问一下,读入有啥问题呀


by dongzirui0817 @ 2024-08-28 21:25:30

@yixingyou scanf 输入的变量不加 & ,你说错哪


by Yxy7952 @ 2024-08-29 09:43:37

@dongzirui0817

谢谢,这种错误我都没看到


|