//等于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. C++多态深入分析!

    以下分析是基于VS2010的.以后会使用G++分析看看G++如何处理多态! // polymorphic_test.cpp : 定义控制台应用程序的入口点. // /** 特别注意:实现C++多态,除 ...

  2. 安装macOS Sierra后怎么找回“任何来源”选项

    安装macOS Sierra后,会发现系统偏好设置的“安全与隐私”中默认已经去除了允许“任何来源”App的选项,无法运行一些第三方应用(提示xx.app已经损坏).如果需要恢复允许“任何来源”的选项, ...

  3. “Hello World!”团队第七周召开的第五次会议

    今天是我们团队“Hello World!”团队第七周召开的第五次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 八.代码 一 ...

  4. cocos2d-x 相关文章资源(安卓开发)

    http://blog.csdn.net/sdhjob/article/details/38734993 http://www.cnblogs.com/code4app/p/4026665.html ...

  5. “吃神么,买神么”的第一个Sprint计划(第二天)

    “吃神么,买神么”项目Sprint计划 ——5.22(第二天)立会内容与进度 团队组员各自任务: 冯美欣:logo的设计.搜索框的制作,"登陆/注册"的文字链接: 吴舒婷:导航条. ...

  6. vim python和golang开发环境配置

    首先在-下新建目录.vim和配置文件.vimrc,.vimrc内容如下: syntax on set nocompatible filetype off set rtp+=~/.vim/bundle/ ...

  7. 深入理解Java虚拟机&运行时数据区

      其中,程序计数器.虚拟机栈.本地方法栈3个区域随线程而生,随线程而灭.

  8. angularJS1笔记-(3)-购物车增删改查练习

    html: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...

  9. Scrum 5.0

    5.0--------------------------------------------------- 1.团队成员完成自己认领的任务. 2.燃尽图:理解.设计并画出本次Sprint的燃尽图的理 ...

  10. 通过session 怎么防止表单的重复提交!

    1.在提交表单的时候使用隐藏域: String tokenValue=new Date().getTime(); <input type="hidden" name=&quo ...