魏老师 @ 2024-05-31 15:38:17
#include<bits/stdc++.h>
using namespace std;
int n,m,q,a[1000010];
int f(int x,int l,int r)
{
if(l==r) return r;
else if(l<r)
{
int mid=(r+l)/2;
if (a[mid]>=x) f(x,l,mid);
else f(x,mid+1,r);
}
}
int main()
{
scanf("%d %d",&n,&m);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]);
for (int i=1;i<=m;i++)
{
scanf("%d",&q);
int ans=f(q,1,n);
if(a[ans]==q) cout<<ans<<" ";
else cout<<-1<<" ";
}
return 0;
}
by Zi_Gao @ 2024-05-31 15:39:19
函数没有返回值
by 魏老师 @ 2024-05-31 15:55:45
@Zi_Gao 明白了,谢谢