关于二分查找

STL函数:

lower_bound(begin,end,index) 返回大于等于index的第一次出现的位置;

upper_bound(begin,end,index)返回大于index的第一次出现的位置。

具体操作如下:

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<algorithm>
 4 using namespace std;
 5 int main()
 6 {
 7     int a[100];
 8     for(int i=0;i<100;i++)
 9         a[i]=i*2;
10     while(1)
11     {
12         int *ans1,*ans2,b;
13         scanf("%d",&b);
14         ans1=lower_bound(a,a+100,b);
15         ans2=upper_bound(a,a+100,b);
16         printf("ans1=%d  ans2=%d\n",*ans1,*ans2);
17     }
18     return 0;    
19 } 

 

posted on 2017-02-06 21:27  左岸zero  阅读(115)  评论(0编辑  收藏  举报

导航