二分查找(等于x,小于x,小于等于x,大于x,大于等于x )
- //等于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 )的更多相关文章
- UVA1152- 枚举 /二分查找
The SUM problem can be formulated as follows: given four lists A,B,C,D of integer values, compute ho ...
- 二分法查找(大于,大于等于x的最小,小于,小于等于x的最大值)
#include<stdio.h> #include<algorithm> using namespace std; //小于x的最大 //大于x的最小 //x的第一个 //x ...
- shell脚本中大于,大于等于,小于,小于等于、不等于的表示方法
症状:shell中大于,大于等于,小于等于,lt,gt ,ne,ge,le 很对应. 应对方法: 大于 -gt (greater than) 小于 -lt (less than) 大于或等于 -ge ...
- 终极二分查找--传说十个人写九个有bug
之前写过一篇极为罗嗦的二分查找,非常得意地以为以后就可以避免踩坑了,但是今天才知道二分查找可以写的既简洁又鲁棒,唉!还是要多学习啊! 给一个按照从大到小的顺序排序好的数组a[]={1,2,3,4,7, ...
- STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())
一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...
- 二分查找(lower_bound和upper_bound)
转载自:https://www.cnblogs.com/luoxn28/p/5767571.html 1 二分查找 二分查找是一个基础的算法,也是面试中常考的一个知识点.二分查找就是将查找的键和子数组 ...
- 笔试算法题(58):二分查找树性能分析(Binary Search Tree Performance Analysis)
议题:二分查找树性能分析(Binary Search Tree Performance Analysis) 分析: 二叉搜索树(Binary Search Tree,BST)是一颗典型的二叉树,同时任 ...
- SUMMARY | 二分查找
package Search; public class biSearch { //标准的二分查找 public static int stdBiSearch(int[] array,int keyV ...
- <二分查找+双指针+前缀和>解决子数组和排序后的区间和
<二分查找+双指针+前缀和>解决子数组和排序后的区间和 题目重现: 给你一个数组 nums ,它包含 n 个正整数.你需要计算所有非空连续子数组的和,并将它们按升序排序,得到一个新的包含 ...
随机推荐
- python实现将json数据以json格式写入txt文件
json.dumps中indent参数是设置json缩进量的 举例: tmp = { "aaa" : "111", "bbb" : '222 ...
- 实践lnmpde 的安装
1.先安装apache, yum install httpd 2.安装MySQL rpm -qa | grep mysql // 这个命令就会查看该操作系统上是否已经安装了mysql数据库 ...
- php 数组去重
php 数组去重 数组中重复项的去除 2010-07-28 15:29 一维数组的重复项: 使用array_unique函数即可,使用实例如下: <?php ...
- 《Spring1之第八次站立会议》
<第八次站立会议> 昨天:我查找了关于实现视频功能的相关代码. 今天:对用C#写的视频功能进行了相关的了解. 遇到的问题:由于对C#不是很了解,所以其中的有些代码还是看不懂.
- NServiceBus官方文档翻译(一)NServiceBus 概况
NServiceBus 概况 NServiceBus 被设计用来组合面向业务的服务,它并不是用来替代诸如 WCF 一类的RPC技术. NServiceBus 不只包含通信模块,像其他成熟的SOA和DD ...
- Enterprise Library 4.1 参考源码索引
http://www.projky.com/entlib/4.1/Microsoft/Practices/EnterpriseLibrary/AppSettings/Configuration/Des ...
- Android之自定义View学习(一)
Android之自定义View学习(一) Canvas常用方法: 图片来源 /** * Created by SiberiaDante on 2017/6/3. */ public class Bas ...
- 第二版_TestNG+Excel+(HTTP+JSON) 简单接口测试
---------------------------------------------------------------------------------------------------- ...
- 关于vue项目管理项目的架构管理平台
关于vue项目管理项目的架构管理平台 https://panjiachen.github.io/vue-element-admin-site/#/zh-cn/faq 31.4k 次浏览 完整项目地址: ...
- apache重写规则 rewrite
Rewrite规则表达式的说明: . 匹配任何单字符 [chars] 匹配字符串:chars [^chars] 不匹配字符串:chars text1|text2 可选择的字符串:text1或text2 ...