折半查找
来源:互联网 发布:node es6 编辑:程序博客网 时间:2024/05/29 09:07
#include<iostream>
using namespace std;
int MiddleSearch(int *l,const int n,int x)
{
int low=0,high=n-1,mid;
while(low<=high)
{
mid = (low+high)/2;
{
if(l[mid]==x)
return mid;
else if(l[mid]<x)
low = mid + 1;
else if(l[mid]>x)
high = mid - 1;
}
}
return -1;
}
void main()
{
int l[8] = {1,2,4,5,6,7,8,9};
int result,e;
while(cin>>e)
{
result = MiddleSearch(l,8,e);
if(result < 0)
cout<<"未找到"<<endl;
else
cout<<e<<"是a["<<result<<"]"<<endl;
}
}
using namespace std;
int MiddleSearch(int *l,const int n,int x)
{
int low=0,high=n-1,mid;
while(low<=high)
{
mid = (low+high)/2;
{
if(l[mid]==x)
return mid;
else if(l[mid]<x)
low = mid + 1;
else if(l[mid]>x)
high = mid - 1;
}
}
return -1;
}
void main()
{
int l[8] = {1,2,4,5,6,7,8,9};
int result,e;
while(cin>>e)
{
result = MiddleSearch(l,8,e);
if(result < 0)
cout<<"未找到"<<endl;
else
cout<<e<<"是a["<<result<<"]"<<endl;
}
}
1 0
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 折半查找
- 适配器方法惯用法
- 奇怪的Java题:为什么1000 == 1000返回为False,而100 == 100会返回为True?
- JAVA SE——流
- 关于C++中的虚拟继承的一些总结
- 史上讲得最清楚的树状数组(至少我是这么认为的)
- 折半查找
- 从头认识设计模式-目录
- 域渗透——Local Administrator Password Solution
- Node.js与J2EE的CQRS框架整合的架构猜想
- Android BLE学习(三):编写自己的 BLE蓝牙读写工具(功能仿照nrf master control panel)
- python与json的转换
- startActivity启动源码
- erlang趣事之guard
- spring 定时器