二分查找(等于x,小于x,小于等于x,大于x,大于等于x )


//等于x
//小于x
//小于等于x
//大于x
//大于等于x


 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <string>
 6 #include <set>
 7 #include <map>
 8 #include <list>
 9 #include <stack>
10 #include <queue>
11 #include <vector>
12 #include <algorithm>
13 #include <iostream>
14 using namespace std;
15 #define ll long long
16 #define minv 1e-6
17 #define inf 1e9
18 const long maxn=1e5+5;
19 const ll mod=1e9+7;
20 
21 //µÈÓÚx
22 //СÓÚx
23 //СÓÚµÈÓÚx
24 //´óÓÚx
25 //´óÓÚµÈÓÚx 
26 
27 long a[maxn];
28 
29 int main()
30 {
31     long n,s,i,l,r,m;
32     scanf("%ld",&n);
33     for (i=1;i<=n;i++)
34         scanf("%ld",&a[i]);
35     scanf("%ld",&s);
36     l=1; r=n;
37     while (l<=r)
38     {
39         m=(l+r)>>1;
40         if (a[m]>=s) //a[l]>=s
41             r=m-1; //a[r]<s
42         else
43             l=m+1;
44     }
45     printf("%ld\n",a[l]);
46     printf("%ld\n",a[r]);
47     /*
48         8 1 1 1 4 4 6 6 6
49         0
50         1 0
51         
52         8 1 1 1 4 4 6 6 6
53         10
54         0 6
55         
56         8 1 1 1 4 4 6 6 6
57         4
58         4 1
59         
60         
61         8 1 1 1 4 4 6 6 6
62         3
63         4 1
64         
65         8 1 1 1 4 4 6 6 6
66         5
67         6 4
68         
69         
70          
71          
72     */
73     return 0;
74 }

 

 

 
  

  a[l]>s a[r]<=s

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <string>
 6 #include <set>
 7 #include <map>
 8 #include <list>
 9 #include <stack>
10 #include <queue>
11 #include <vector>
12 #include <algorithm>
13 #include <iostream>
14 using namespace std;
15 #define ll long long
16 #define minv 1e-6
17 #define inf 1e9
18 const long maxn=1e5+5;
19 const ll mod=1e9+7;
20 
21 //µÈÓÚx
22 //СÓÚx
23 //СÓÚµÈÓÚx
24 //´óÓÚx
25 //´óÓÚµÈÓÚx 
26 
27 long a[maxn];
28 
29 int main()
30 {
31     long n,s,i,l,r,m;
32     scanf("%ld",&n);
33     for (i=1;i<=n;i++)
34         scanf("%ld",&a[i]);
35     scanf("%ld",&s);
36     l=1; r=n;
37     while (l<=r)
38     {
39         m=(l+r)>>1;
40         if (a[m]>s) //a[l]>s
41             r=m-1; //a[r]<=s
42         else
43             l=m+1;
44     }
45     printf("%ld\n",a[l]);
46     printf("%ld\n",a[r]);
47     /*
48         8 1 1 1 4 4 6 6 6
49         0
50         1 0
51         
52         8 1 1 1 4 4 6 6 6
53         10
54         0 6
55         
56         8 1 1 1 4 4 6 6 6
57         4
58         6 4
59         
60         
61         8 1 1 1 4 4 6 6 6
62         3
63         4 1
64         
65         8 1 1 1 4 4 6 6 6
66         5
67         6 4
68         
69         
70          
71          
72     */
73     return 0;
74 }

 

若要判断相等,则

Code1 : if l!=n+1 && a[l]==s

Code2 : if r!=0 && a[r]==s

 

转载于:https://www.cnblogs.com/cmyg/p/9086537.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值