二分查找及lower_bound与upper_bound
2013-08-14 14:32
330 查看
二分查找(迭代实现)
二分查找
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
const int maxn=1000;
int a[maxn],n,t[maxn],p;
int bsearch(int *a,int x,int y,int n)
{
int m;
while(x<y)
{
m=x+(y-x)/2;
if(a[m]==n)return m;
else if(a[m]>n)y=m;
else x=m+1;
}
return -1;
}
int main()
{
while(~scanf("%d",&n))
{ for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int v;
scanf("%d",&v);
int k=bsearch(a,0,n,v);
printf("%d\n",k);
}
}
运用二分查找求下界(lower_bound)
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
const int maxn=1000;
int a[maxn],n,t[maxn],p;
int lower_bound(int *a,int x,int y,int n)
{
int m;
while(x<y)
{
m=x+(y-x)/2;
if(a[m]>=n)y=m;
else x=m+1;
}
return x;
}
int main()
{
while(~scanf("%d",&n))
{ for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int v;
scanf("%d",&v);
int k=lower_bound(a,0,n,v);
printf("%d\n",k);
}
}
相对的求上界(upper_bound)只需对上面代码稍作修改
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
const int maxn=1000;
int a[maxn],n,t[maxn],p;
int lower_bound(int *a,int x,int y,int n)
{
int m;
while(x<y)
{
m=x+(y-x)/2;
if(a[m]<=n)x=m+1;
else y=m;
}
return x;
}
int main()
{
while(~scanf("%d",&n))
{ for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int v;
scanf("%d",&v);
int k=lower_bound(a,0,n,v);
printf("%d\n",k);
}
}
注意:用上面两个函数可以求出一个数在序列中所在位置的二区间,但用lower_bound与upper_bound两个函数求出来的区间是[l,r)的半闭半开区间,不用误用为单纯的闭区间(为什么留给读者自己思考)
二分查找
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
const int maxn=1000;
int a[maxn],n,t[maxn],p;
int bsearch(int *a,int x,int y,int n)
{
int m;
while(x<y)
{
m=x+(y-x)/2;
if(a[m]==n)return m;
else if(a[m]>n)y=m;
else x=m+1;
}
return -1;
}
int main()
{
while(~scanf("%d",&n))
{ for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int v;
scanf("%d",&v);
int k=bsearch(a,0,n,v);
printf("%d\n",k);
}
}
运用二分查找求下界(lower_bound)
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
const int maxn=1000;
int a[maxn],n,t[maxn],p;
int lower_bound(int *a,int x,int y,int n)
{
int m;
while(x<y)
{
m=x+(y-x)/2;
if(a[m]>=n)y=m;
else x=m+1;
}
return x;
}
int main()
{
while(~scanf("%d",&n))
{ for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int v;
scanf("%d",&v);
int k=lower_bound(a,0,n,v);
printf("%d\n",k);
}
}
相对的求上界(upper_bound)只需对上面代码稍作修改
#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
const int maxn=1000;
int a[maxn],n,t[maxn],p;
int lower_bound(int *a,int x,int y,int n)
{
int m;
while(x<y)
{
m=x+(y-x)/2;
if(a[m]<=n)x=m+1;
else y=m;
}
return x;
}
int main()
{
while(~scanf("%d",&n))
{ for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int v;
scanf("%d",&v);
int k=lower_bound(a,0,n,v);
printf("%d\n",k);
}
}
注意:用上面两个函数可以求出一个数在序列中所在位置的二区间,但用lower_bound与upper_bound两个函数求出来的区间是[l,r)的半闭半开区间,不用误用为单纯的闭区间(为什么留给读者自己思考)
相关文章推荐
- 使用std::lower_bound和std::upper_bound解决常见的二分查找问题
- c++ 二分查找的函数 lower_bound & upper_bound & binary_search
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
- STL 二分查找 upper_bound和lower_bound用法
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
- STL之二分查找(binary_search(),lower_bound(),upper_bound() )
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
- 二分查找,lower_bound,upper_bound
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
- STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
- STL之二分查找(binary_search(),lower_bound(),upper_bound() )
- 二分查找:binary_search、lower_bound和upper_bound的实现
- C++ STL 二分查找函数(lower_bound 和 upper_bound)
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
- STL之二分查找(binary_search(),lower_bound(),upper_bound() )
- STL中的二分查找——lower_bound 、upper_bound 、binary_search