当前位置:网站首页>上课笔记(5)(1)——#593. 二分查找(binary)
上课笔记(5)(1)——#593. 二分查找(binary)
2022-07-28 01:03:00 【xyc20120615】
Description
给出有 n 个元素的由小到大的序列,请你编程找出某元素第一次出现的位置。(n≤10^6)
Format
Input
第一行:一个整数,表示由小到大序列元素个数;
下面 n 行,每行一个整数;
最后一行一个整数 x,表示待查找的元素;
Output
如果 x 在序列中,则输出 x 第一次出现的位置,否则输出 -1。
Samples
输入数据 1
5
3
5
6
6
7
6
输出数据 1
3
Limitation
1s, 1024KiB for each test case.
方法一:
看这名字就知道要用二分,但我不信邪,试了试暴力搜,竟然AC了!,不得不吐槽一下,那个OJ的数据是真的水。
程序:
#include<bits/stdc++.h>//不建议使用本程序,很有可能爆零
using namespace std;
int n,x,a[1000010];
int main(){
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
cin>>x;
for(int i=1;i<=n;i++)
if(a[i]==x){
cout<<i;
return 0;
}
coutt<<-1;
return 0;
}方法二:
就是正常的二分查找,大致思路就是开两个变量设定范围然后根据变的之间值不断缩小范围纸质找到,注意在数组是有序的情况下才能进行二分,要么就先排序。
程序:
#include<bits/stdc++.h>
using namespace std;
int n,x,a[1000010];
int main(){
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
cin>>x;
int l=1,r=n;
while(l<=r){
int mid=(l+r)/2;
if(a[mid]<x)
l=mid+1;
else
r=mid-1;
}
if(a[l]!=x){
cout<<-1;
return 0;
}
cout<<l;
return 0;
}PS:方法一请谨慎使用!
边栏推荐
- 数据输出-绘制动图
- Xiaomi website homepage big module - small module + navigation (floating case)
- Typescript中类的使用
- Flex layout - fixed positioning + flow layout - main axis alignment - side axis alignment - expansion ratio
- 都在说DevOps,你真正了解它吗?
- [深入研究4G/5G/6G专题-42]: URLLC-14-《3GPP URLLC相关协议、规范、技术原理深度解读》-8-低延时技术-2-基于slot的调度与Slot内灵活的上下行符号配比
- 清除浮动的原因和六种方法(解决浮动飞起影响父元素和全局的问题)
- Sample imbalance - entry 0
- Flex development web page instance web side
- 重要安排-DX12引擎开发课程后续直播将在B站进行
猜你喜欢

【数据库数据恢复】SQL Server数据库磁盘空间不足的数据恢复案例

LeetCode 热题 HOT 100 -> 2.两数相加
![Likeshop takeout ordering system [100% open source, no encryption]](/img/e6/a73aa817b5b30339d755aa53708072.png)
Likeshop takeout ordering system [100% open source, no encryption]

云原生爱好者周刊:Prometheus 架构演进之路

Traversal and properties of binary trees

Behind every piece of information you collect, you can't live without TA

How to evaluate the effectiveness of R & D personnel? Software Engineer reports help you see everyone's contribution

小米网站主页面大模块——小模块+导航(浮动案例)

Promise from getting started to mastering (Chapter 3: customize (handwriting) promise)

结构伪类选择器—查找单个—查找多个—nth-of-type和伪元素
随机推荐
Ceresdao: the world's first decentralized digital asset management protocol based on Dao enabled Web3.0
Principle and implementation of cross entropy
LeetCode 热题 HOT 100 -> 3. 无重复字符的最长子串
Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
Likeshop takeout ordering system [100% open source, no encryption]
Four common post data submission methods
C # using ABP warehouse to access the database error record set
样本不均衡-入门0
Promise from introduction to mastery (Chapter 4 async and await)
Structure pseudo class selector - find single - find multiple - nth of type and pseudo elements
Flume(5个demo轻松入门)
Principle and implementation of focal loss
数据输出-图片注释、标注
结构伪类选择器—查找单个—查找多个—nth-of-type和伪元素
A lock faster than read-write lock. Don't get to know it quickly
小程序毕设作品之微信校园维修报修小程序毕业设计成品(4)开题报告
Unittest unit test framework full stack knowledge
记录一次生产死锁
【网站搭建】使用acme.sh更新ssl证书:将zerossl改为letsencrypt
Zkrollup learning materials summary