C语言 二分查找

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int i;
    int x;
    int a[8];
    for(i=0;i<8;i++)
    cin>>a[i];
    printf("aa:");
    cin>>x;
    int l,h,m;
    l=0;
    h=7;
    while(l<=h)
    {
        m=(l+h)/2;
        if(a[m]>x)
        {
            h=m-1;
        }
        else
        if(a[m]<x)
        {
            l=m+1;
        }
        else
        break;
    }
    if(a[m]==x)cout<<1<<endl;
    else
    cout<<0<<endl;
}

猜你喜欢

转载自blog.csdn.net/qq_42778026/article/details/88781203
今日推荐