//等于x
//小于x
//小于等于x
//大于x
//大于等于x
 #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long
#define minv 1e-6
#define inf 1e9
const long maxn=1e5+;
const ll mod=1e9+; //µÈÓÚx
//СÓÚx
//СÓÚµÈÓÚx
//´óÓÚx
//´óÓÚµÈÓÚx long a[maxn]; int main()
{
long n,s,i,l,r,m;
scanf("%ld",&n);
for (i=;i<=n;i++)
scanf("%ld",&a[i]);
scanf("%ld",&s);
l=; r=n;
while (l<=r)
{
m=(l+r)>>;
if (a[m]>=s) //a[l]>=s
r=m-; //a[r]<s
else
l=m+;
}
printf("%ld\n",a[l]);
printf("%ld\n",a[r]);
/*
8 1 1 1 4 4 6 6 6
0
1 0 8 1 1 1 4 4 6 6 6
10
0 6 8 1 1 1 4 4 6 6 6
4
4 1
8 1 1 1 4 4 6 6 6
3
4 1 8 1 1 1 4 4 6 6 6
5
6 4 */
return ;
}

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

 #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
#define ll long long
#define minv 1e-6
#define inf 1e9
const long maxn=1e5+;
const ll mod=1e9+; //µÈÓÚx
//СÓÚx
//СÓÚµÈÓÚx
//´óÓÚx
//´óÓÚµÈÓÚx long a[maxn]; int main()
{
long n,s,i,l,r,m;
scanf("%ld",&n);
for (i=;i<=n;i++)
scanf("%ld",&a[i]);
scanf("%ld",&s);
l=; r=n;
while (l<=r)
{
m=(l+r)>>;
if (a[m]>s) //a[l]>s
r=m-; //a[r]<=s
else
l=m+;
}
printf("%ld\n",a[l]);
printf("%ld\n",a[r]);
/*
8 1 1 1 4 4 6 6 6
0
1 0 8 1 1 1 4 4 6 6 6
10
0 6 8 1 1 1 4 4 6 6 6
4
6 4
8 1 1 1 4 4 6 6 6
3
4 1 8 1 1 1 4 4 6 6 6
5
6 4 */
return ;
}

若要判断相等,则

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. 【神经网络】自编码聚类算法--DEC (Deep Embedded Clustering)

    1.算法描述 最近在做AutoEncoder的一些探索,看到2016年的一篇论文,虽然不是最新的,但是思路和方法值得学习.论文原文链接 http://proceedings.mlr.press/v48 ...

  2. linux, configure --prefix 的作用

    指定安装路径不指定prefix,则可执行文件默认放在/usr /local/bin,库文件默认放在/usr/local/lib,配置文件默认放在/usr/local/etc.其它的资源文件放在/usr ...

  3. 2017年软件工程第十二次作业-PSP总结报告

    回顾1 1.回想一下你曾经对计算机专业的畅想 当初你是如何做出选择计算机专业的决定的?经过一个学期,你的看法改变了么,为什么? 你认为过去接触到的课程是否符合你对计算机专业的期待,为什么?经过一个学期 ...

  4. PSP总结报告1

    回答作业问题 1.回想一下你曾经对计算机专业的畅想 我高考后报考的是计算机科学与技术,当时对计算机技术基本了解为零,当时以为什么东西都会用到计算机,学计算机以后不会找不到工作,刚开学的时候对计算机一窍 ...

  5. golang type

    参考链接 https://blog.csdn.net/tzs919/article/details/53571632 type是golang中非常重要的关键字,常见的就是定义结构体,但是其功能远不止是 ...

  6. Pytorch相关内容

    ---恢复内容开始--- Pytorch中文官方文档:https://pytorch-cn.readthedocs.io/zh/latest/package_references/torch-nn P ...

  7. 关于Keil C51中“ERROR L107: ADDRESS SPACE OVERFLOW ”的总

    最近写一个关于单片机播放音乐的程序,出现如下错误: *** ERROR L107: ADDRESS SPACE OVERFLOW ... ... Program Size: data=167.6 xd ...

  8. GC 年轻代 老年代 持久代

    转载自:http://www.cnblogs.com/yaoyuan23/p/5587548.html 虚拟机中的共划分为三个代:年轻代(Young Generation).老年代(Old Gener ...

  9. js访问对象属性的两种方法

    var obj={name:'fuuf',age:19} 第一种,用.访问 obj.name 第二种 用[]访问 obj['name']  //此时name是字符串,要加引号 注意事项 使用第二种方法 ...

  10. [BUAA_SE_2017]个人阅读作业 + 总结

    个人阅读作业 银弹 银弹是指能让狼人一枪毙命的致命子弹,对于软件工程而言,我觉得是不存在银弹的.每一项软件开发都是极为特殊的,有特定的需求.特定的功能,如果存在银弹能够直击要害解决问题,那么软件的开发 ...