【深基13.例1】查找0分求调

P2249 【深基13.例1】查找

zhy519 @ 2025-01-10 21:46:53

今天刚学完二分我STL以经打过了,自己打了一个手写代码,调了好久还是0分(各位大佬修改时不要用STL)

#include <iostream>

using namespace std;

const long long N = 10001000;

long long n, m;
int q[N];

int main()
{
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; i ++ ) scanf("%d", &q[i]);

    while (m -- )
    {
        int x;
        scanf("%d", &x);

        long long l = 0, r = n - 1;
        while (l < r)
        {
        int mid = (l + r) / 2;
        if (q[mid] >= x)
            {
                r = mid;
            } else {
                l = mid + 1;
            }
        }

        if (q[l] != x) cout << "−1"<< endl;
        else
        {
            cout << l + 1 << endl;
        }
    }
}

by LionBlaze @ 2025-01-10 21:50:29

\beta 表示能用 STL 为啥不用


by LionBlaze @ 2025-01-10 21:52:04

@zhy519 scanf("%d %d", &n, &m); \to scanf("%d%d", &n, &m);,虽然我不知道为啥错但以前这样打过寄了


|