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

📄 二分查找数程序.cpp

📁 算法的几个程序 排序 货郎档 二分法
💻 CPP
字号:
#include <iostream>
using namespace std;

int s[10]={00,11,22,33,44,55,66,77,88,99};
int n=0,x=9,m,a;                              
void f(int i,int j);

int main(){
	cout<<"输入要找的数:"<<endl;
	cin>>a;
	f(n,x);
	return 0;
}

void f(int i,int j){
	m=(i+j)/2;
	if(a<s[i]||a>s[j]){
        cout<<"NOT EXIST!"<<endl;
	}
	else if(a==s[i]){
		cout<<"要查找的数的位置为:"<<i+1<<endl;
	}
	else if(a==s[j]){
		cout<<"要查找的数的位置为:"<<j+1<<endl;
	}
	else if(a==s[m]){
		cout<<"要查找的数的位置为:"<<m+1<<endl;
	}
	else if(i==j&&s[m]!=a){
		cout<<"要找的数不在数组中!"<<endl;
	}
	else if(s[i]<a&&s[m]>a){
		f(i+1,m-1);
	}
	else if(s[m]<a&&s[j]>a){
		f(m+1,j-1);
	}

}

⌨️ 快捷键说明

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