0分求助(哭)

P2249 【深基13.例1】查找

tmcz202070101 @ 2024-04-30 19:45:10

#include<bits/stdc++.h>
#include <algorithm>
using namespace std;
int n,m,x,a[1000005];
int find(int q)
{
    int l=0,r=n+1;
    while(l+1<r){
        int mid=(l+r)/2;
        if(a[mid]>=q) r=mid;
        else l=mid;
    }
    return a[r]==q ? r: -1;
}
int main()
{
    scanf("%d.%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=m;i++)
    {
        scanf("%d",&x);
        printf("%d ",find(x));
    }
}

by fanjiayu666 @ 2024-04-30 20:08:35

这题要用结构体存id和值,题目要求输出id, 二分也不大对。

int find(int q)
{
    int l=1,r=n,mid=(l+r)/2;
    while(l<=r){
        mid=(l+r)/2;
        if(a[mid]./*(值)*/>=q) r=mid-1;
        else l=mid+1;
    }
    if(a[l].val==x)return a[l]./*(id)*/;
    return -1;
}

by fanjiayu666 @ 2024-04-30 20:09:52

错了

int find(int q)
{
    int l=1,r=n,mid=(l+r)/2;
    while(l<=r){
        mid=(l+r)/2;
        if(a[mid]./*(值)*/>=q) r=mid-1;
        else l=mid+1;
    }
    if(a[l]./*(值)*/==q)return a[l]./*(id)*/;
    return -1;
}

by dongzhen @ 2024-05-13 14:03:15

@fanjiayu666 错啦,不是这个原因

@tmcz202070101 楼主

你没输出空格!!!太可爱了


by tmcz202070101 @ 2024-05-17 18:01:06

@dongzhen 好的好的,谢谢!


|