//等于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. Sqlmap常用命令大全

    1 Options(选项) -h,--help 显示帮助消息-hh 显示详细帮助-version -v VERBOSE 详细级别 0-6 默认12 Target 目标-u URL--url=URL-g ...

  2. Python更新库

    查看系统里过期的python库,可以用pip命令 [root@vnode33 sim-enb-sgi]# pip list #列出所有安装的库 Package Version ------------ ...

  3. B2

    组员1:吴晓晖(组长) 过去两天完成了哪些任务 代码重构基本完成 展示GitHub当日代码/文档签入记录 接下来的计划 推荐算法 还剩下哪些任务 组员2:陈锦谋 过去两天完成了哪些任务 重新制作图标 ...

  4. 【CS231N】2、多类SVM

    一.疑问 1. assignments1 linear_svm.py文件的函数 svm_loss_naive中,使用循环的方式实现梯度计算 linear_svm.py文件的函数 svm_loss_ve ...

  5. 20181126-java-面试知识-收集

    链接:最近面试java后端开发的感受:如果就以平时项目经验来面试,通过估计很难——再论面试前的准备 两本书的链接:Java Web轻量级开发面试教程和Java核心技术及面试指南.

  6. Software Defined Networking(Week 3, part 1)

    Control and Data Plane Seperation 课程地址 Overview 今天正式地学习一个控制与数据平面分离.学习完本节,应该要掌握何为控制和数据平面,了解它们的功能以及好处, ...

  7. Good Time 冲刺 五

    一.今日完成任务情况及遇到的问题 第五天 日期:2018.6.18 王怡镔:今天继续在学习中完善编写页面,对之前的页面进行部分改进. 于鑫宇:修改完善布局,复习. 胡雅馨:今天继续改进页面,努力解决时 ...

  8. Swift-KVC构造函数中数据类型和私有属性

  9. 开放地址法散列表ADT

    数据结构定义如下: typedef unsigned int Index; typedef Index Position; struct HashTbl; typedef struct HashTbl ...

  10. fidder监控请求响应时间和请求IP(摘抄至网络)

    增加监控请求的详情时间 在CustomRules.js的class Handlers中增加  //添加请求的响应时间 public static BindUIColumn("Time Tak ...