当前位置:网站首页>1152: binary search
1152: binary search
2022-04-21 09:10:00 【Qian Qian】
1152: Two point search
Title Description
Find an element in an ordered sequence x.
Input
First enter a positive integer n(n<=100000), Indicates that the sequence has n It's an integer , Then enter... In descending order n It's an integer ;
Then there is a positive integer m, Express m Search times ;
And finally m It's an integer , Express m An integer to find x.
Output
For every search , There's a line of output . If there is an element to look for in the sequence x, Output element x The sequence number in the sequence ( The serial number from 0 Start ); If there is no element to look for in the sequence x, The output "Not found!".
The sample input Copy
5
1 3 5 7 9
11
-1
1
2
3
4
5
6
7
8
9
10
Sample output Copy
Not found!
0
Not found!
1
Not found!
2
Not found!
3
Not found!
4
Not found!
source / classification
#include<stdio.h>
#include<string.h>
#define N 100000
/* Two point search low<=high x>a[mid] Find... On the right , namely low=mid+1 x<a[mid] Find on the left , namely high=mid-1 x=a[mid] You can return the query results */
int a[N];
int efind(int a[],int n,int k)
{
int low=0,high=n-1,mid;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==k) return mid;
else if(a[mid]<k) low=mid+1;
else high=mid-1;
}
return -1;
}
int main(){
int n,m,r=0,x;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
scanf("%d",&m);
while(m--){
scanf("%d",&x);
r=efind(a,n,x);
if(r!=-1) printf("%d\n",r);
else printf("Not found!\n");
}
return 0;
}
版权声明
本文为[Qian Qian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210906132115.html
边栏推荐
猜你喜欢

Opencv -- separate color channel, image contrast, brightness adjustment, discrete Fourier transform (10)

Dark blue - Visual slam - Section 6 exercise

postman测试Excel文件导入导出功能

Handler asynchronous message passing mechanism (I) common basic usage of handler

CC10000.CloudJenkins—————————————

2022 chemical automation control instrument examination exercises and simulation examination
![Buuctf [geek challenge 2019] havefun](/img/5a/9b1942c0d24f079be804c7b3304be1.png)
Buuctf [geek challenge 2019] havefun

Pipy MQTT 代理之(三)Logging
![Buuctf [actf2020 freshman competition] include](/img/20/3c0d0286c869385ee0c9a119e337e2.png)
Buuctf [actf2020 freshman competition] include

Note 0104 MySQL advanced - index - Overview
随机推荐
Notes of the most complete grain mall in the whole network_ 02. Introduction to the overall effect of the project (2022-04-02)
BUUCTF[HCTF 2018]WarmUp
ACTF2020 新生赛 Upload 1
A summary of school training
二叉树知识
Intranet penetration - proxy penetration - rights lifting - injection - MSF Middleware - domain penetration - log clearing - learning resources
性能分析思路
Getting started with object detection FAQs (deep learning / image classification)
规划自己的健康问题
Handler asynchronous message passing mechanism (I) common basic usage of handler
C 100 points secret script sduwh
Penetration test - roaming from public cloud to intranet rce deserialization FRP
In 2017, I also started to write CSDN blog (Sina Netease moved to CSDN)
Map Object WeakMap
2017我也开始真正写CSDN博客(新浪网易转战CSDN)
1167: 逆转数(指针专题)
模拟实现vector
极客大挑战 2019 Upload 1
CC00019.CloudJenkins—————————————
Flink的api入门案例