二分查找代码模板,自取

· · 个人记录

#include<bits/stdc++.h>
using namespace std;
int main(){
    int ans=4;
    int l=1,r=3,mid;
    while(l<=r){
        mid=(l+r)/2;
        if(mid==ans){
            cout<<"找到了";
            break;
        }else if(mid>ans){
            r=mid-1;
            cout<<"太大了"<<endl;
        }else{
            l=mid+1;
            cout<<"太小了"<<endl;
        }
    }

    return 0;
}