本地测试能过,但提交全WA。

P2249 【深基13.例1】查找

ssczLuJingping @ 2023-07-13 22:38:28

调了半小时,还是过不了。

代码如下:

#include<bits/stdc++.h>
using namespace std;
    const int maxn = 1e6 + 5;
    int n, m, a[maxn];
int main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= m; i++) {
        int x;
        cin >> x;
        int left = 1, right = n+1;
        while (left != right) {
            int mid = (right + left) / 2;
            if (a[mid] < x)left = mid + 1;
            else right = mid;
        }
        if (a[left] == x)cout << left << ' ';
    }
    cout << -1 << ' ';
    return 0;
}

题解我看过了,基本差不多,最后仿照题解过了,但就是过不了。。

求大神指点一二!


by LOSpace @ 2023-07-13 23:50:46

while (left != right) 改成 while (left < right)


by ssczLuJingping @ 2023-07-14 08:21:43

@moonspace 感谢大佬!


|