AC狗题解
2024-12-28 10:01:45
发布于:浙江
5阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int x;
cin >> x;
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (a[mid] == x) {
cout << mid + 1 << endl; // 下标从 1 开始
return 0;
} else if (a[mid] < x) {
left = mid + 1;
} else {
right = mid - 1;
}
}
cout << -1 << endl;
return 0;
}
全部评论 1
顶
2024-12-28 来自 浙江
0
有帮助,赞一个