样例过了,9WA1T求调

P2249 【深基13.例1】查找

Li_wc0802 @ 2023-11-26 18:30:44

#include<iostream>
using namespace std;
int n,m,a[1000005],q;
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",&q);
        int l,r,mid,flag=-1;
        l=1;r=n;
        while(l<r)
        {
            mid=(l+r)/2;
            if(a[mid]==q)
            {
                int t=mid;
                while(a[t]==q)t--;
                flag=t+1;
                break;
            }
            else
            {
                if(q>a[mid])
                    l=mid+1;
                else 
                    r=mid-1;
             } 
        }
        printf("%d ",flag);
    }
    return 0;
}

by __zfy__ @ 2023-11-26 18:48:22

@Li_wc 我可以交一下你的代码吗?帮你测试,本题我已AC。


by Li_wc0802 @ 2023-11-26 18:50:03

@zfy 可以感谢大佬!!


by __zfy__ @ 2023-11-26 18:51:40

@Li_wc 首先你讲讲你的思路,我们私聊


by __zfy__ @ 2023-11-26 18:57:13

@Li_wc 看私信


by MsgNotFound @ 2023-11-26 19:01:04

#include<iostream>
using namespace std;
int n,m,a[1000005],q;
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",&q);
        int l,r,mid,flag=-1;
        l=1;r=n;
        while(l<=r)
        {
            mid=(l+r)/2;
            if(a[mid]==q)
            {
                int t=mid;
                while(a[t]==q)t--;
                flag=t+1;
                break;
            }
            else
            {
                if(q>a[mid])
                    l=mid+1;
                else 
                    r=mid-1;
             } 
        }
        printf("%d ",flag);
    }
    return 0;
}

64pts

@Li_wc


|