这个用二分查找怎么做(已迷一个多小时了)
#include<stdio.h>int main()
/*{
int array[10];
int low,high,mid,target;
scanf("%d",&target);
while(low<=high)
{
int mid=(low+high)/2;
if(array[mid]>target)
high=mid-1;
else if(array[mid]<target)
low=mid+1;
else
return mid;
}
return-1;
}*/
{
int a[10];
int i,n,mid,x,low,high;
scanf("%d",&n);
for(i=1;i<=n;++i)
{
scanf("%d",&a[i]);
}
scanf("%d",&x);
low=1;
high=n;
while(low<=high)
{
mid=(high+low)/2;
if(a[mid]>x)
high=mid-1;
else if(a[mid]<x)
low=mid+1;
else
return mid;
}
return x;
}
/*for(i=1;i<=n;++i)
{
mid=(1+n)/2;
if(x>a[mid])
a[1]=a[mid+1];
else if(x<a[mid])
a[n]=a[mid-1];
if(x==a[mid])
{
printf("%d",mid);
break;
}
}
return x;
}*/