Kobe_Bryan @ 2024-07-16 09:17:08
全
#include<bits/stdc++.h>
using namespace std;
int a[1000001],b[100001];
int main()
{
int x,y,m=-1;
bool g=false;
scanf("%d%d",&x,&y);
for(int i=1;i<=x;i++) scanf("%d",&a[i]);
for(int i=1;i<=y;i++) scanf("%d",&b[i]);
for(int i=1;i<=y;i++)
{
if(a[x/2]>b[i])
{
for(int j=1;j<=a[x/2];j++)
if(a[i]==b[j])
{
g=true;
m=i;
break;
}
}
else if(a[x/2]<b[i])
{
for(int j=a[x/2];j<=y;j++)
if(a[i]==b[j])
{
g=true;
m=i;
break;
}
}
else
{
cout<<a[x/2]<<" ";
break;
}
if(g)cout<<m<<' ';
else cout<<m;
m=-1;
}
return 0;
}
by ace_yulong @ 2024-07-16 09:22:20
孩子参考一下吧
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+9;
int n,m,a[N];
int main()
{
cin>>n>>m;
for (int i=1;i<=n;i++){
cin>>a[i];
}
while(m--){
int q;
cin>>q;
int l=1,r=n,ans=-1;
while(l<=r){
int mid=(l+r)/2;
if (a[mid]>=q){
r=mid-1;
if (a[mid]==q){
ans=mid;
}
}
else{
l=mid+1;
}
}
cout<<ans<<" ";
}
return 0;
}
by _ChongYun_ @ 2024-07-16 09:30:20
@Kobe_Bryan 做法显然假的。萌新的话请先学习一下二分。
by Andy_anqutuwei @ 2024-07-21 10:50:26
@kobe_Bryan 二分呀
by Andy_anqutuwei @ 2024-07-21 10:51:17
@Andy_anqutuwei 6