二分查找(等于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 //D?óúx
23 //D?óúμèóú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 //D?óúx
23 //D?óúμèóú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

时间: 2024-08-29 17:03:15

二分查找(等于x,小于x,小于等于x,大于x,大于等于x )的相关文章

二分查找原理和实现

前言 的确,我一开始的时候也认为二分查找挺简单的,但是在我对二分查找进行总结的时候,发现虽然思路很简单,但是代码要写的正确就不容易了. 区间 需要注意的是 注意计算的区间是左闭右开区间[)还是左闭右闭区间[],两者的代码是不太一样的. 左闭右闭区间 如果说你使用的是左闭右闭区间: int search3(int array[], int n, int target) { int left = 0; int right = n - 1; //比如说只有一个情况下 while (left <= ri

(二分查找思想)从有序递增旋转数组45679123 中找到数字6的位置

#define _CRT_SECURE_NO_WARNINGS 1 #include <iostream> using namespace std; /** * 从有序递增旋转数组45679123 中找到数字6的位置 * 数组递增 但有旋转 * 二分查找思想 * 时间复杂度小于O(N) * {7,8,9,10,1,2,3,4,5,6} *************/ int find_revolve_array(const int arr[], int len, int value) { if

UVA1152- 枚举 /二分查找

The SUM problem can be formulated as follows: given four lists A,B,C,D of integer values, compute how many quadruplet (a,b,c,d) ∈ A×B×C×D are such that a+b+c+d = 0. In the following, we assume that all lists have the same size n. InputThe input begin

二分查找,查指定值、小于k的最大值,大于k的最大值

我们经常会用到二分查找 二分查找应该很多人都会写了,今天要写一个用二分查找找到小于k的最大值的时候看了很久不懂他设计的思路,后来想通了,记录一下. 所以这篇主要是讲 用二分查找找到小于k的最大值和大于k的最大值. 二分查找查找指定值 这个挺简单的,直接上代码吧 //获取值是k的位置,找不到则返回-1 public static int getK(int[] a, int k){ if(a.length == 0){ return -1; } int l = 0; int r = a.length

二分查找、二分查找大于等于key的第一个元素、二分查找小于等于key的最后一个元素

二分查找很简单,二分查找的变形需要注意一些细节. 1.当找大于等于key的第一个元素,或者查找小于等于key的最后一个元素时, 循环条件是 low < high,这和基本的二分查找不同, 但需要在循环退出的时候,判断是否满足条件: 2.如果是找最后一个满足条件的情况, 下限移动时不能用 low=mid+1:而应该用 low=mid: 此时,mid计算时应该用 mid=(low+high+1)/2, 保证 最后low.high相差1时不会陷入死循环, 循环退出后,下限可能是结果: 3.如果是找第一

二分查找总结

最近刷leetcode和lintcode,做到二分查找的部分,发现其实这种类型的题目很有规律,题目大致的分为以下几类: 1.最基础的二分查找题目,在一个有序的数组当中查找某个数,如果找到,则返回这个数在数组中的下标,如果没有找到就返回-1或者是它将会被按顺序插入的位置.这种题目继续进阶一下就是在有序数组中查找元素的上下限.继续做可以求两个区间的交集. 2.旋转数组问题,就是将一个有序数组进行旋转,然后在数组中查找某个值,其中分为数组中有重复元素和没有重复元素两种情况. 3.在杨氏矩阵中利用二分查

二分查找小结

在弄dp时感觉一道题需非要弄清二分查找不可.以前学二分一直就很迷惑,网上资料也各种各样.的确二分是个很容易写错的算法,今天只好不算太耐心的再看一遍二分.总感觉时间不够用.. 二分查找有许多细节,这次先抓主要矛盾.关于什么(left+rigth)/2溢出的问题啊先不考虑了.对我来说二分迷惑的地方还是在1.while(left?right) ?处到底是<还是<= 2.判断后mid到底是加一还是减一还是不变? 3.返回left还是right? 这次大概明白了一些,因为二分查找是看区间开闭的,对于左闭

养成良好的编程风格--论二分查找的正确姿势

摘自:http://www.cnblogs.com/ider/archive/2012/04/01/binary_search.html 在学习算法的过程中,我们除了要了解某个算法的基本原理.实现方式,更重要的一个环节是利用big-O理论来分析算法的复杂度.在时间复杂度和空间复杂度之间,我们又会更注重时间复杂度. 时间复杂度按优劣排差不多集中在: O(1), O(log n), O(n), O(n log n), O(n2), O(nk), O(2n) 到目前位置,似乎我学到的算法中,时间复杂度

二分查找的实现和应用汇总(转载)

转载地址:http://www.cnblogs.com/ider/archive/2012/04/01/binary_search.html 二分查找法的实现和应用汇总 在学习算法的过程中,我们除了要了解某个算法的基本原理.实现方式,更重要的一个环节是利用big-O理论来分析算法的复杂度.在时间复杂度和空间复杂度之间,我们又会更注重时间复杂度. 时间复杂度按优劣排差不多集中在: O(1), O(log n), O(n), O(n log n), O(n2), O(nk), O(2n) 到目前位置