全WA

P2249 【深基13.例1】查找

sqrt_PI @ 2024-12-14 10:05:25

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

by thh_0621 @ 2024-12-14 11:53:28

define int long long

int a[1000100]; int n, m, x;

int check(int 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||a[r]==x) return l; else return -1; }

signed main() { cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; } while (m--) { cin >> x; printf("%lld ", check(x)); } return 0; }

求关

#define int long long
int a[1000100];
int n, m, x;

int check(int 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||a[r]==x) return l;
    else    return -1;
}

signed main() {
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    while (m--) {
        cin >> x;
        printf("%lld ", check(x));
    }
    return 0;
}

by sqrt_PI @ 2024-12-16 21:10:19

虽然看不懂一部分,但包关的。


|