请大佬看看,怎样判断数组没有这个数

P2249 【深基13.例1】查找

yanghaoyu123 @ 2024-11-02 21:53:30

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    int n,m;
    cin>>n>>m;
    int arr[n+1];
    int a[m+1];
    for (int i=1;i<=n;i++){
        cin>>arr[i];
    }
    for (int i=1;i<=m;i++){
        cin>>a[i];
    }
    for (int i=1;i<=m;i++){
        int mid=n;
        while (1){
            if (a[i]<arr[mid]){
                mid=ceil((double)mid/2);
            }
            else if (a[i]>arr[mid]){
                mid=ceil((double)mid/2);
                mid=mid+ceil((double)mid/2);
            }
            if (a[i]==arr[mid]){
                cout<<mid<<" ";
                break;
            }
            if (a[i]>arr[n] || a[i]<arr[1] || mid==1 || mid==n){
                cout<<-1<<" ";
                break;
            }
        }
    }
    return 0;
}

by a123456a0000000000 @ 2024-11-03 11:00:53

你这是二分吗?lr呢?


|