1.输入一组有序数列;
2.每次查找序列的中间位置并与目标数比较;
3.依据比较缩小数列,直到找到目标数或数列长度为1;
4.输出;
#include<iostream>
using namespace std;
int n,t,flag;
int a[100];
int f(int l, int r)
{
while(l<r)
{
int mid =l+r>>1;
if (a[mid]>=t)r=mid;
else l=mid+1;
}
flag=l;
return a[l];
}
int main(){
cin>>n>>t;
for(int i=0;i<n;i++)cin>>a[i];
if(f(0,n-1)==t){printf("目标数在第%d个",flag+1);}
else{cout<<"Not Found";}
return 0;
}
标签:二分,数列,int,mid,else,flag,查找,day From: https://www.cnblogs.com/drz1145141919810/p/17329148.html