1. //等于x
    //小于x
    //小于等于x
    //大于x
    //大于等于x
  2.  
  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+;
  19. const ll mod=1e9+;
  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=;i<=n;i++)
  34. scanf("%ld",&a[i]);
  35. scanf("%ld",&s);
  36. l=; r=n;
  37. while (l<=r)
  38. {
  39. m=(l+r)>>;
  40. if (a[m]>=s) //a[l]>=s
  41. r=m-; //a[r]<s
  42. else
  43. l=m+;
  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. 8 1 1 1 4 4 6 6 6
  61. 3
  62. 4 1
  63.  
  64. 8 1 1 1 4 4 6 6 6
  65. 5
  66. 6 4
  67.  
  68. */
  69. return ;
  70. }
  1.  

  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+;
  19. const ll mod=1e9+;
  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=;i<=n;i++)
  34. scanf("%ld",&a[i]);
  35. scanf("%ld",&s);
  36. l=; r=n;
  37. while (l<=r)
  38. {
  39. m=(l+r)>>;
  40. if (a[m]>s) //a[l]>s
  41. r=m-; //a[r]<=s
  42. else
  43. l=m+;
  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. 8 1 1 1 4 4 6 6 6
  61. 3
  62. 4 1
  63.  
  64. 8 1 1 1 4 4 6 6 6
  65. 5
  66. 6 4
  67.  
  68. */
  69. return ;
  70. }

若要判断相等,则

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

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

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

  1. UVA1152- 枚举 /二分查找

    The SUM problem can be formulated as follows: given four lists A,B,C,D of integer values, compute ho ...

  2. 二分法查找(大于,大于等于x的最小,小于,小于等于x的最大值)

    #include<stdio.h> #include<algorithm> using namespace std; //小于x的最大 //大于x的最小 //x的第一个 //x ...

  3. shell脚本中大于,大于等于,小于,小于等于、不等于的表示方法

    症状:shell中大于,大于等于,小于等于,lt,gt ,ne,ge,le 很对应. 应对方法: 大于 -gt (greater than) 小于 -lt (less than) 大于或等于 -ge ...

  4. 终极二分查找--传说十个人写九个有bug

    之前写过一篇极为罗嗦的二分查找,非常得意地以为以后就可以避免踩坑了,但是今天才知道二分查找可以写的既简洁又鲁棒,唉!还是要多学习啊! 给一个按照从大到小的顺序排序好的数组a[]={1,2,3,4,7, ...

  5. STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())

    一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search  -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...

  6. 二分查找(lower_bound和upper_bound)

    转载自:https://www.cnblogs.com/luoxn28/p/5767571.html 1 二分查找 二分查找是一个基础的算法,也是面试中常考的一个知识点.二分查找就是将查找的键和子数组 ...

  7. 笔试算法题(58):二分查找树性能分析(Binary Search Tree Performance Analysis)

    议题:二分查找树性能分析(Binary Search Tree Performance Analysis) 分析: 二叉搜索树(Binary Search Tree,BST)是一颗典型的二叉树,同时任 ...

  8. SUMMARY | 二分查找

    package Search; public class biSearch { //标准的二分查找 public static int stdBiSearch(int[] array,int keyV ...

  9. <二分查找+双指针+前缀和>解决子数组和排序后的区间和

    <二分查找+双指针+前缀和>解决子数组和排序后的区间和 题目重现: 给你一个数组 nums ,它包含 n 个正整数.你需要计算所有非空连续子数组的和,并将它们按升序排序,得到一个新的包含 ...

随机推荐

  1. python实现将json数据以json格式写入txt文件

    json.dumps中indent参数是设置json缩进量的 举例: tmp = { "aaa" : "111", "bbb" : '222 ...

  2. 实践lnmpde 的安装

    1.先安装apache, yum install httpd 2.安装MySQL rpm -qa | grep mysql       // 这个命令就会查看该操作系统上是否已经安装了mysql数据库 ...

  3. php 数组去重

    php 数组去重 数组中重复项的去除 2010-07-28 15:29 一维数组的重复项: 使用array_unique函数即可,使用实例如下: <?php                    ...

  4. 《Spring1之第八次站立会议》

    <第八次站立会议> 昨天:我查找了关于实现视频功能的相关代码. 今天:对用C#写的视频功能进行了相关的了解. 遇到的问题:由于对C#不是很了解,所以其中的有些代码还是看不懂.

  5. NServiceBus官方文档翻译(一)NServiceBus 概况

    NServiceBus 概况 NServiceBus 被设计用来组合面向业务的服务,它并不是用来替代诸如 WCF 一类的RPC技术. NServiceBus 不只包含通信模块,像其他成熟的SOA和DD ...

  6. Enterprise Library 4.1 参考源码索引

    http://www.projky.com/entlib/4.1/Microsoft/Practices/EnterpriseLibrary/AppSettings/Configuration/Des ...

  7. Android之自定义View学习(一)

    Android之自定义View学习(一) Canvas常用方法: 图片来源 /** * Created by SiberiaDante on 2017/6/3. */ public class Bas ...

  8. 第二版_TestNG+Excel+(HTTP+JSON) 简单接口测试

    ---------------------------------------------------------------------------------------------------- ...

  9. 关于vue项目管理项目的架构管理平台

    关于vue项目管理项目的架构管理平台 https://panjiachen.github.io/vue-element-admin-site/#/zh-cn/faq 31.4k 次浏览 完整项目地址: ...

  10. apache重写规则 rewrite

    Rewrite规则表达式的说明: . 匹配任何单字符 [chars] 匹配字符串:chars [^chars] 不匹配字符串:chars text1|text2 可选择的字符串:text1或text2 ...