The binary search method finds the number in the array that a number is. If there is no such number, it will output "No such number found".

#include <iostream>
using namespace std;
int main()
{     int a[15]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,145};     cout<<"Please enter the number you want to search for";     int n;     cin>>n;     int l=0;     int m=14;//Search from the end and the beginning;     int h;//Search in half      while (l<m) {         h=(l+m)/2;         if(a[h]==n)         break;//stop searching after finding; exit the loop         else if(a[h]>n) //upper limit Change         m=h-1;         else         l=h+1;     }     cout<<"is the "<<h+1<<" number";     return 0; }















        



Guess you like

Origin blog.csdn.net/weixin_62802660/article/details/122539919