#include<iostream>
using namespace std;
#include<cstdlib>
int BinSearch(int r[],int n,int k);
int BinSearch(int r[],int n,int k)
{
int low=1,high=n;
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(k<r[mid])high=mid-1;
else if(k>r[mid])low=mid+1;
else return mid;
}
return 0;
}
int main()
{
int i,k,location;
int a[5]={12,36,45,68,78};
location=0;
for(i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"请输入要查找的数:"<<endl;
cin>>k;
location=BinSearch(a,5,k);
cout<<"元素"<<k<<"在序列中的序号是:"<<location<<endl;
return 0;
}
查找
最新推荐文章于 2024-05-06 17:11:49 发布