?? erfenfa2.cpp
字號:
//二分查找法(非遞歸調用)erfenfa2.cpp
#include<iostream.h>
#include<iomanip.h>
void binsrch(int a[],int n,int x)
{int mid,top=0,bot=n-1,i,find=0,m=0;
do {
m=m+1;
mid=(top+bot)/2;
if(a[mid]==x)
{cout<<x<<"在序列中找到!\n";find=1;}
else if(x<a[mid]) bot=mid-1;
else if(x>a[mid]) top=mid+1;
}while((top<=bot)&&(find==0));
if(find==0) cout<<x<<"在序列中未找到!\n";
cout<<"查找次數:"<<m<<endl;}
void main()
{cout<<"erfenfa2運行結果:\n";
int b[]={1,3,5,7,9,10,12,15,17,19};
int x=9,n=10;
cout<<"輸入要查找的x:";cin>>x;
cout<<"查找結果:\n";
binsrch(b,n,x);
cin.get();cin.get();}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -