#include<iostream>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
int bsearch(int* ans,int left,int right,int value)//仅仅查找是否存在某值等于已知值,所以分大于小于等于三种情况即可;
{
while(right>left)
{
int middle=left+(right-left)/2;
if(ans[middle]==value) return middle;
else if(ans[middle]>value) right=middle;
else left=middle+1;
}
return left;
}
int lsearch(int* ans,int left,int right,int value)//实现lower_bound,寻找第一个不小于的值,所以如果小于,left在middle的基础上加上一,大于等于让right等于middle;
{
while(right>left)
{
int middle=left+(right-left)/2;
if(ans[middle]<value) left=middle+1;
else right=middle;
}
return left;
}
int usearch(int* ans,int left,int right,int value)//实现upper_bound,寻找第一个大于的值,所以如果小于等于就让left加一,大于就让right等于middle;
{
while(right>left)
{
int middle=left+(right-left)/2;
if(ans[middle]<=value) left=middle+1;
else right=middle;
}
return left;
}
int main()
{
int ans[10]={1,3,4,8,9,23,23,23,89,101};
cout<<ans[usearch(ans,0,10,23)]<<endl;
return 0;
}
#include<iomanip>
#include<algorithm>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<cstdio>
using namespace std;
int bsearch(int* ans,int left,int right,int value)//仅仅查找是否存在某值等于已知值,所以分大于小于等于三种情况即可;
{
while(right>left)
{
int middle=left+(right-left)/2;
if(ans[middle]==value) return middle;
else if(ans[middle]>value) right=middle;
else left=middle+1;
}
return left;
}
int lsearch(int* ans,int left,int right,int value)//实现lower_bound,寻找第一个不小于的值,所以如果小于,left在middle的基础上加上一,大于等于让right等于middle;
{
while(right>left)
{
int middle=left+(right-left)/2;
if(ans[middle]<value) left=middle+1;
else right=middle;
}
return left;
}
int usearch(int* ans,int left,int right,int value)//实现upper_bound,寻找第一个大于的值,所以如果小于等于就让left加一,大于就让right等于middle;
{
while(right>left)
{
int middle=left+(right-left)/2;
if(ans[middle]<=value) left=middle+1;
else right=middle;
}
return left;
}
int main()
{
int ans[10]={1,3,4,8,9,23,23,23,89,101};
cout<<ans[usearch(ans,0,10,23)]<<endl;
return 0;
}