UVA
来源:互联网 发布:统计分析软件r 编辑:程序博客网 时间:2024/04/30 01:07
第五章的第一个大理石的题,附上书上的代码
#include<cstdio>#include<algorithm>using namespace std;const int maxn=10000;int main(){ int n,q,x,a[maxn],kase=0; while(scanf("%d%d",&n,&q)==2&&n) { printf("CASE# %d:\n",++kase); for(int i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); while(q--) { scanf("%d",&x); int p=lower_bound(a,a+n,x)-a; if(a[p]==x)printf("%d found at %d\n",x,p+1); else printf("%d not found\n",x); } } return 0;}这个题里面有一个stl函数 lower_bound,这个函数参数为(数组起始位置,数组终止位置,所需要寻找的数x)—数组名,
函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置
举例如下:
一个数组number序列为:4,10,11,30,69,70,96,100.设要插入数字3,9,111.pos为要插入的位置的下标
则
pos = lower_bound( number, number + 8, 3) - number,pos = 0.即number数组的下标为0的位置。
pos = lower_bound( number, number + 8, 9) - number, pos = 1,即number数组的下标为1的位置(即10所在的位置)。
pos = lower_bound( number, number + 8, 111) - number, pos = 8,即number数组的下标为8的位置(但下标上限为7,所以返回最后一个元素的下一个元素)。
所以,要记住:函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的!!~
返回查找元素的第一个可安插位置,也就是“元素值>=查找值”的第一个元素的位置
0 0
- uva
- UVA
- UVA
- UVA
- uva
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- UVA
- 笔记:图像分割算法之 Graph Cut
- [Java]理解JVM之二:类加载器
- Objective-c NSString和char*之间的转换
- bean转json时null字段不转换的方法
- 紫书搜索 习题7-1 UVA
- UVA
- POJ 2579 Blurred Vision G++
- 渲染引擎杂记(1)——浮点数误差分析及补偿
- 简单的数位dp是怎样运行的
- 统计整个工程代码行数
- 蓝桥杯-第七届Java语言B组-剪邮票
- Linux下如何用互斥锁实现Windows下的临界区功能
- 1001. 害死人不偿命的(3n+1)猜想 (15)
- 测试选择排序算法性能