//等于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. 简单理解DNS解析流程(一)

    0x0 简单理解dns DNS服务器里存着一张表 表中放着域名和IP地址,域名和IP地址以映射关系保存,即一对一 浏览器访问某个域名,实际上是访问它的ip地址 所以浏览器需要知道域名对应的ip地址 如 ...

  2. 使用cors解决跨域遇到浏览器发出options嗅探

    前言: 本地开发起的服务器,通过修改hosts文件设置域名映射到本地,接口在测试环境 1. 服务器端设置cors, 配置access-control-allow-origin 头部 使用蚂蚁金服的up ...

  3. kafka handler

    1.配置kafka 参数文件 在ogg主目录下有示例文件: [root@WH0PRDBRP00AP0013 ogg]# cd AdapterExamples/big-data/kafka/ [root ...

  4. nginx模块学习

    rewrite模块的语法 链接

  5. C++ 类 构造函数 constructor

    构造函数 当定义了一个整型变量: int a; 这会申请了一块内存空间来存储a,但是这块内存中原本有数据的,可能是任何值,这不是你所希望的,若你就希望a表示1,所以要把a的值赋值为1. ; 例: #i ...

  6. 团队博客作业Week2 --- 学长学姐访谈录

    ## 团队作业2 ## ### 团队一 ### 这个团队中组员是位研一的学姐,她的软件工程老师是姚淑珍,当时她们团队总共有4个人,而且她们都很努力,但是可能是最后团队的作品不太理想,她们的软件并没有上 ...

  7. MathExam作业

    作业 一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 50 40 • Estimate • ...

  8. 第二阶段Sprint冲刺会议2

     进展:讨论主界面布局,跳转界面的布局,查看有关页面跳转的资料及示例代码并试着编写. 

  9. sqlDataAdapter和SqlCommand的区别

    因为DataSet是离线的,所以SqlDataAdapter这个对象是连接DataSet和数据库的桥梁,所有对DataSet的操作(填充,更新等)都要通过他 ado.net数据访问有两种方式: 1.离 ...

  10. My Sql数据库设置环境变量和字符集

    一.踩坑背景 之前开发中一直用的是sql  server 数据库,最近接到公司一个老的项目,用的my sql数据库做的,功能做了一大部分,现在客户要求对原程序和数据库进行服务器的迁移工作.产品经理给出 ...