yi18600361898 @ 2024-03-17 12:41:27
#include <bits/stdc++.h>
using namespace std;
int main(){
long long n,m,q;
cin>>n>>m;
long long a[n];
int low=0,high=n-1,mid=(low+high)/2;
for(long long i=0;i<n;i++)
cin>>a[i];
for(long long i=0;i<m;i++){
cin>>q;
while(low<=high){
if(a[mid]==q){
cout<<mid+1<<" ";
break;
}
else if(a[mid]>q){
low=mid+1;
mid=(low+high)/2;
}
else if(a[mid]<q){
high=mid-1;
mid=(low+high)/2;
}
}
cout<<"-1 ";
}
return 0;
}
by xiangzhenze611 @ 2024-03-17 12:45:49
@yi18600361898 while
循环错了
if(a[mid]==q){
cout<<mid+1<<" ";
break;
}
else if(a[mid]>q){
high=mid-1;
mid=(low+high)/2;
}
else if(a[mid]<q){
low=mid+1;
mid=(low+high)/2;
}
by xiangzhenze611 @ 2024-03-17 12:46:28
@yi18600361898 还有,这样查找出来的不一定是第一次出现的位置
by xiangzhenze611 @ 2024-03-17 12:48:32
@yi18600361898 例如输入为:
5 1
1 1 1 1 1
1
你的输出是 3
正确输出为 1
by yi18600361898 @ 2024-03-17 13:15:35
@xiangzhenze611
真不会了 555