1. 程式人生 > >啊啊啊 為啥輸出16

啊啊啊 為啥輸出16

#include <stdio.h>
#include <stdlib.h>

int BinarSearch(int a[],int x,int n)
{
int left=0;
int right =n-1;
while(left<right-1)
{
int middle =(left+right)/2;
if(x<a[middle])right =middle;
else left =middle;

}
if(x==a[left]) return left;
return -1;

}

int main()
{
printf(“Hello world!\n”);
int a[100]={1,2,5,8,12,15,16,18,20,22};
for(int i=0;i<10;i++)
printf("%d",a[i]);
printf("\n");
int j = BinarSearch(a,22,10);
printf("%d",a[j]);

return 0;

}