//等于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. 转载:GBDT算法梳理

    学习内容: 前向分布算法 负梯度拟合 损失函数 回归 二分类,多分类 正则化 优缺点 sklearn参数 应用场景 转自:https://zhuanlan.zhihu.com/p/58105824 G ...

  2. Xavier——Understanding the difficulty of training deep feedforward neural networks

    1. 摘要 本文尝试解释为什么在深度的神经网络中随机初始化会让梯度下降表现很差,并且在此基础上来帮助设计更好的算法. 作者发现 sigmoid 函数不适合深度网络,在这种情况下,随机初始化参数会让较深 ...

  3. golang slice使用不慎导致的问题

    原文链接 : http://www.bugclosed.com/post/16 背景 go语言中切片slice是方便且好用的强大数据结构,但是使用的时候需要注意,不然容易出问题,最近因为遇到了一个sl ...

  4. 慢吞吞的pip切换源

    http://blog.csdn.net/gz_liuyun/article/details/52778198

  5. Scrum Meeting 11.05

    成员 今日任务 明日计划 用时 徐越 代码移植 学习ListView+simpleAdapter,actionBar.阅读并修改前端代码 4h 赵庶宏 服务器配置,代码移植  构建后端数据库,进行完善 ...

  6. 【每日scrum】第一次冲刺day2

    和小伙伴一起找地图 ,学习了mapinfo地图格式的基本知识,数据和图像分开存储

  7. web01-helloworld

    新建一个Java web项目,名字为web01 在项目中新建一个servlet,名字为SimpleHello 修改doGet()方法,为: public void doGet(HttpServletR ...

  8. 冲刺One之站立会议6 /2015-5-19

    2015-5-19 今天把服务器端的界面完善了一下,然后大家查了好多资料,实现了登陆界面实际连接的功能,开始加了一个它和服务器的的跳转,但是分析过后发现这是个没有必要的跳转.登录应该直接转到聊天室的主 ...

  9. 作业45//Calculator::3.0

    计算器 github 我的天我到底要写什么 一,2.0及2.6的改动 做了计算部分 加入了判断输入是否合法 合法的定义是算式符合`数字+运算符+数字+运算符+数字`的格式 其中`"-&quo ...

  10. 第三周作业(一)VS安装及单元测试练习

    第三周作业(一) 需求:练习教科书第22~25页单元测试练习,要求自行安装Visual Studio开发平台,版本至少在2010以上,要求把程序安装过程和练习过程写到博客上,越详细越好,要图文并茂,没 ...