二分板子

ZhangFirst1發表於2024-04-05

二分板子

#include <iostream>

constexpr int N = 100;

int main(){
    int a[N];
    int n;
    std::cin >> n;
    for(int i=0; i<n; i++) std::cin >> a[i];
    int l = 0, r = n - 1;
    int t;
    std::cin >> t;

    while(l < r){
        int mid = (l + r) >> 1;
        if(t > a[mid]) l = mid + 1;
        else r = mid;
    }

    if(a[l] == t){
        std::cout << "Found it, Located in:" << l+1 << std::endl;
    }else{
        std::cout << "Not Found" << std::endl;
    }
}