P2249 【深基13.例1】查找

传送门

这道题是二分查找,找到第一个符合的数在序列的下标
可以用STL,在这里我手写一下

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int n,m,x;
int a[maxn];
int binary(int l,int r,int x){
	int mid,ans=0;
	while(l<=r){
		mid=l+(r-l)/2;
		if(a[mid]>x)r=mid-1;
		else if(a[mid]<x)l=mid+1;
		else ans=mid,r=mid-1;
	}
	if(!ans)
	return -1;
	else return ans;
}
int main(){
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	scanf("%d",&a[i]);
	while(m--){
		scanf("%d",&x);
		printf("%d ",binary(1,n,x));
	}
	cout<<endl;
	return 0;
}
发布了115 篇原创文章 · 获赞 3 · 访问量 1755

猜你喜欢

转载自blog.csdn.net/qq_43721152/article/details/104989869