``
//二分查找 计数二班 许纪龙 2018204299
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
int BS(int t[],int x)
{
int m,l=0,r=sizeof(t)-1; //要想具体实现下表l需要从0开始
sort(t,t+(sizeof(t)-1));//'0'的存在数组长度减1
while(l<r)
{m=floor((l+r)/2);
if (t[m]==x)
{return m;
}
if(t[m]>x)
{r=m-1;
}
if(t[m]<x)
{l=m+1;
}
}
return 0;
}//二分法递归
int main()
{
int n,x,e;
printf("输入要排序的数组长度");
scanf("%d",&n);
int t[n];
printf("输入数组");
for(int i=0;i<n;i++)
{scanf("%d",&t[i]);
}
printf("输入查找元素");
scanf("%d",&x);
e=BS(t,x);
printf("%d",e);
return 0;
}