首页 > 其他 > 详细

二分查找 变形

时间:2014-05-18 18:49:53      阅读:386      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
#include<stdio.h>

int solver(const int a[],const int n,const int t)
{
  int total = 0;
  if (NULL == a && 0 >= n)
      return total;
  int start = 0;
  int end = n-1;
  while(start <= end)
  {
   printf("%d   %d\n",start,end);
   int middle = (start + end)/2;
   if(t == a[middle])
   {
     total = 1;
     int cursor = 0;
     while(++cursor && middle + cursor < n && t == a[middle + cursor])
       total++;
     cursor = 0;
     while(--cursor && middle + cursor >= 0 && t == a[middle + cursor])
        total++;
     return total;
   }
   else if(t > a[middle])
    end = middle - 1;
   else
    start = middle + 1;
  }
  return total;
}

int main()
{
    int a[] = {1,1,1,1,1,1};
    int b[] = {1,2,2,2,2,3};
    int c[] = {1,2,3,5,6,7};
    printf("%d",solver(b,6,4));
}
bubuko.com,布布扣

 

二分查找 变形,布布扣,bubuko.com

二分查找 变形

原文:http://www.cnblogs.com/berkeleysong/p/3733954.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!