比较好理解的代码空格隔开了三个输出的判定
2023-08-14 16:58:19
发布于:四川
8阅读
0回复
0点赞
#include<iostream>
using namespace std;
int n,x,a[100005],minn;
int main(){
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
cin>>x;
int y;
for(int i=1;i<=x;i++){
cin>>y;
int l=1,r=n,mid,ans=0;
while(l<=r){
mid=(l+r)>>1;
if(a[mid]>y) r=mid-1;
else if(a[mid]<y) l=mid+1;
else{
ans=mid;
r=mid-1;
}
}
if(!ans) cout<<-1<<" ";
else cout<<ans<<" ";
l=1,r=n,ans=0;
while(l<=r){
mid=(l+r)>>1;
if(a[mid]>y) r=mid-1;
else if(a[mid]<y) l=mid+1;
else{
ans=mid;
l=mid+1;
}
}
if(!ans) cout<<-1<<" ";
else cout<<ans<<" ";
l=1,r=n,minn=r+1;
while(l<=r){
mid=(l+r)>>1;
if(a[mid]>y){
minn=min(minn,mid);
r=mid-1;
}else if(a[mid]<=y){
l=mid+1;
}
}
if(minn<n+1) cout<<minn<<endl;
else cout<<-1<<endl;
}
return 0;
}
这里空空如也
有帮助,赞一个