⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 401.cpp

📁 折半查找算法,实现对于一组数据的查找操作,利用折半查找法进行查找
💻 CPP
字号:
template<class T>
int chazhao(T a[],int low,int hight,T key)
{
	int mid;
	if(low<hight)
		return -1;
	else
	{	mid=(low+hight)/2;
	    if(key==a[mid])
			return mid;
		if(key<mid)
			return chazhao(a,low,mid,key);
/*不可写成chazhao(T a[],int low,int mid,int key)或chazhao(a[],low.mid,key)*/
		if(key>mid)
			return chazhao(a,mid,hight,key);
	}
}
#include"iostream.h"
#include"fstream.h"
void main()
{
	int a[50];
	int word;
	int pos,i;
	ifstream fin;
	fin.open("zj.dat");
	if(!fin)
	{
		cerr<<"can't open"<<endl;
	}
	i=0;
	while(fin>>a[i])  //fin>>a[i]
	{	i++;cout<<a[i]<<endl;;}		/*文件?*/
	cout<<"enter a number:";
	cin>>word;
	if(pos=chazhao(a,0,i,word)!=-1)
		cout<<word<<"is in the pos of"<<pos<<endl;
	else
		cout<<"not in"<<endl;
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -