大佬们能都帮我看看问题在哪,0分

P2249 【深基13.例1】查找

ZJdog @ 2024-04-02 18:46:10

#include<bits/stdc++.h>
using namespace std;
const int N=1000010;
int n,m;
int a[N],b[N];

int find_2(int x,int l,int r){
    while(l<r){
        int mid=l+r>>1;
        if(a[mid]>=x){
            r=mid;
        }else l=mid+1;
    }
    if(a[l]!=x){
        l=-1;
    }
    printf("%d ",l);

}

int main(){
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int j=1;j<=m;j++){
        scanf("%d",&b[j]);
    }
    for(int k=1;k<=m;k++){
        find_2(b[k],1,n);
    }
    return 0;
}

by HEROBRINEH @ 2024-04-02 18:48:34

AC 求关


#include<stdio.h>
const int N = 1e6 + 5;
int n, m;
int q[N];
int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%d", &q[i]);
    while (m--)
    {
        int x;
        scanf("%d", &x);

        int l = 1, r = n ;
        while (l < r)
        {
            int mid = l + r>>1;
            if (q[mid] >= x) r = mid; 
            else l = mid + 1;
        }
        if (q[l] != x) printf("-1 ");
        else printf("%d ", l);
    }
    return 0;
}

by cpulxb @ 2024-04-03 08:13:43

你写的每什么毛病,错在细节上,函数没有返回值,不能写int,写成void才行


|