紫书章五 大理石在哪 UVA 10474(stl排序查找)
来源:互联网 发布:女士斜挎包 淘宝 编辑:程序博客网 时间:2024/04/28 06:19
这个题的要求是先排序,后查找。然后书上是用了 sort 和 lower_bound函数都在algorithm头文件里
sort函数是一个模板函数,可以对任意对象进行排序,不一定是内置类型。还可以改变其排序规则。
lower_bound():查找大于或者等于x的第一个位置。
upper_bound(): 查找大于x的第一个位置
比如数组为 a[5]=1,2,3,3,3
然后查找0,1,3
int p=lower_bound(a,a+5,0)-a;//p=0
int p=upper_bound(a,a+5,0)-a;// p=0
int p=lower_bound(a,a+5,1)-a;//p=0;
int p=upper_bound(a,a+5,1)-a;//p=1;
int p=lower_bound(a,a+5,3)-a;//p=2;
int p=upper_bound(a,a+5,3)-a;//p=5;
然后就是这道题的代码
#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>using namespace std;const int maxn=10000;int a[maxn];int main(){ int n,m,case1=0; while(scanf("%d %d",&n,&m)!=EOF&&n+m) { memset(a,0,sizeof(a)); for(int i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); printf("CASE# %d:\n",++case1); for(int i=0;i<m;i++){ int q; scanf("%d",&q); int p=lower_bound(a,a+n,q)-a; if(a[p]==q) printf("%d found at %d\n",q,p+1); else printf("%d not found\n",q); } } return 0;}
0 0
- 紫书章五 大理石在哪 UVA 10474(stl排序查找)
- UVa 10474 大理石在哪?
- UVa 10474 大理石在哪?
- 大理石在哪儿 (排序和查找) UVA
- UVA 10474 大理石在哪里
- 大理石在哪儿(UVa 10474)(排序与检索)
- 大理石在哪(Where is the Marble?, uva 10474)
- Uva 10474(lower_bound()的使用) 大理石在哪
- 大理石在哪?(Where is the Marble?, UVa 10474)代码
- UVaOJ 10474 大理石在哪?
- UVa 10474 大理石在哪儿
- 算法之路二:刘汝佳算法竞赛入门经典:STL排序与检索 大理石在哪 UVA10474
- 例题5-1 UVA 10474 Where is the Marble?大理石在哪?
- 大理石在哪
- 大理石在哪
- 例题:大理石在哪儿(UVa 10474)
- UVa 10474 Where is the Marble?(大理石在哪儿)
- 大理石在哪儿(Where is the marble?,UVa 10474)
- 【LeetCode71-80】71目录简化72DP问题73巧妙利用0位置76通用substr问题精妙模板79DFS+回溯
- Android中资源管理机制详细分析
- DEV grid分组
- 面向过程和面向对象
- gcc、make、makefile、cmake、cmakelists区别
- 紫书章五 大理石在哪 UVA 10474(stl排序查找)
- 递推
- JavaOOP 实现多态 加 简单工厂
- 题解:String to Integer (atoi)
- Week Training: 542 01 Matrix
- LeetCode 513. Find Bottom Left Tree Value
- 使用FIFO来进行两个进程间的通信
- cssRule那些事儿
- Vector的简单使用