Copyright (c) 2015,烟台大学计算机学院
All rights reserved,
All rights reserved,
文件名称:idea.cpp
作者:王阳
完成日期:2015年12月18日
#include<iostream>
using namespace std;
int main()
{
int low=0,high,n;
int a[100];
int i=0,f;
cout<<"Please input the number of data and what data do you want to find"<<endl;
while(cin>>n)
{
cin>>f;
high=n-1;
while(i<n)
{
cin>>a[i];
i++;
}
int mid;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==f)
{
cout<<"find in the postion "<<mid+1<<endl;
break;
}
if(a[mid]>f)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
if(low>high)
{
cout<<"can't find it"<<endl;
}
cout<<"Please input the number of data and what data do you want to find"<<endl;
low=0;
i=0;
}
return 0;
}
本文介绍了一段使用C++实现的二分查找算法,包括输入数据数量和目标值,通过循环查找并输出目标值所在位置或未找到提示。
559

被折叠的 条评论
为什么被折叠?



