题解
2023-08-14 14:10:20
发布于:浙江
4阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
int main() {
int n ,m ,x;
cin >> n >> m;
int lst[n + 1] = {};
for (int i = 1 ; i <= n ; i++) //输入列表
cin >> lst[i];
for (int i = 1 ; i <= m ; i++){
int l = 1 , r = n;
bool f = 0;
cin >> x;
while (l <= r){ //二分法查找
int mid = l + (r - l) / 2;
if (lst[mid] == x){
cout << mid << "\n";
f = 1;
break;
}
else if (lst[mid] < x)
l = mid + 1;
else
r = mid - 1;
}
if (f == 0)
cout << -1 << "\n";
}
return 0;
}
这里空空如也
有帮助,赞一个