题解
2024-10-13 15:21:33
发布于:广东
1阅读
0回复
0点赞
#include<bits/stdc++.h>
using namespace std;
int a[110];
int main(){
int n,x;
cin >> n;
for(int i = 1;i <= n;i++){
cin >> a[i];
}
cin >> x;
int left = 1,right = n;
while(left <= right){
int mid = (left + right) / 2;
if(a[mid] == x){
cout << mid;
return 0;
}else if(a[mid] > x){
right = mid - 1;
}else{
left = mid + 1;
}
}
cout << -1;
return 0;
}
这里空空如也
有帮助,赞一个