#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[1010];
int main()
{
int n,k;
while(scanf("%d",&n)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d",&k);
int t1=lower_bound(a,a+n,k)-a;//将k插入a数组中子序列保持递增,能获得最小的数组下标,从左向右插;
int t2=upper_bound(a,a+n,k)-a;//将k插入a数组中子序列保持递增,能获得最大的数组下标,从右向左插;
bool t3=binary_search(a,a+n,k); //只有1或0代表在a数组中能否查到k;
printf("%d %d %d\n",t1,t2,t3);
}
return 0;
}
STL 二分查找
最新推荐文章于 2023-02-12 10:58:15 发布