能过样例 提交全WA 大牛帮帮

P2249 【深基13.例1】查找

M15388049303 @ 2024-03-26 13:24:14

☞( ヮ゚)☞代码の如下☜(゚ヮ゚☜)↓↓↓←←

#include <bits/stdc++.h>
#define MAXN 1000010
using namespace std;
int n, m,q,a[MAXN];
bool find(int x) {
    int l = 1, r = n;
    while(l<=r){
        int mid=(l+r)/2;
        if(a[mid]==x)return mid;
        else if(a[mid]>x)r=mid-1;
        else l=mid+1;
    }
    return -1;
}
int main() {
    cin>>n>>m;
    for(int i=1;i<=n;++i)
        cin>>a[i];
    for(int i=0;i<m;++i){
        cin>>q;
        cout<<find(q)<<" ";
    }
    return 0;
}

by Lg2307 @ 2024-03-26 20:55:37

@M15388049303

#include <bits/stdc++.h>
using namespace std;
const int z = 1e6 + 10;
int a[z];
int main(){
    int n,m;
    int x;
    cin >> n >> m;
    for (int i = 1; i <= n; i ++){
        cin >> a[i];
    }
    while (m --){
        cin >> x;
        int l = 1,r = n;
        while (l < r){
            int mid = (l + r) / 2;
            if (a[mid] >= x){
                r = mid;
            }
            else {
                l = mid + 1;
            }
        }
        if (a[l] == x){
            cout << l << " ";
        }
        else {
            cout << "-1" << " ";
        }
    }
    return 0;
}

为什么不发到这里


by M15388049303 @ 2024-03-27 17:55:19

@Lg2307,因为想发这儿~~~。

本贴完


by M15388049303 @ 2024-03-27 17:59:28

@Lg2307

T436732 合并遍历

咋做呢??


|