#include<stdio.h>
int main()
{
int element_count(int * set, int len, int e);
int set[10]={1,2,3,4,5,5,5,5,8,9};
printf("%d\n",element_count(set,10,9));
}
int element_count(int * set, int len, int e)
{
int mid,low=0,high=len-1;
int num=0;
while (low<=high)
{
mid=(low+high)/2;
//如果高低等值
if(set[low]==set[high])
{
if(set[mid]==e)
{return high-low+1;
break;}
else
{return 0;break;}
}
//高低值不等
if(set[mid]==e)
{
if(set[low]==e)
{
num=num+(mid-low+1);
low=mid+1;
}
else if(set[high]==e)
{
num=num+(high-mid+1);
high=mid-1;
}
else
{
high--;
}
}
else if(set[mid]>e)
{
high=mid-1;
}
else
{
low=mid+1;
}
}
return num;
}