ProgramAlgTrain/20240919/CSP常考算法模板/二分查找_加速查询.cpp

45 lines
472 B
C++
Raw Normal View History

2024-09-19 10:54:22 +00:00
#include <algorithm>
2024-09-19 02:22:41 +00:00
#include <bits/stdc++.h>
using namespace std;
int a[11]={5, 13, 19, 21, 37, 56, 64, 75, 80, 88, 92};
int main()
{
2024-09-19 10:54:22 +00:00
//C++ stl 二分查找
// std::upper_bound()
// std::lower_bound()
2024-09-19 02:22:41 +00:00
int x;
cin>>x;
int l=0,r=10;
int ans;
while(l<=r)
{
int mid=(l+r)/2;
if(x==a[mid])
{
cout<<mid;
return 0;
}
if(x<a[mid])
{
r=mid-1;
}
else
{
ans=mid;
l=mid+1;
}
}
cout<<"Not found!";
return 0;
}
//5
//1 -2 3 1 -4