UVa 10474 Where is the Marble?(大理石在哪儿)
来源:互联网 发布:魔兽世界178数据库 编辑:程序博客网 时间:2024/04/30 09:16
UVa 10474 Where is the Marble?(大理石在哪儿)
UVa题目链接
题目名称:大理石在哪
题目描述:
现有N个大理石,每个大理石上写了一个非负整数,首先把各数从小到大排序,之后回答Q个问题,每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪块大理石上写着x,排序后的大理石从左到右编号为1~N。
样例输入:
4 1
2 3 5 1
5
5 2
1 3 3 3 1
2 3
样例输出:
CASE# 1:
5 found at 4
CASE# 2:
2 not found
3 found at 3
题目分析:
①输入 第一个数的个数,第二个需要找大理石的个数 输出查找情况
②本题主要需要做两个事,第一个是排序,第二个是查找,所以我们可以把我们输入的数放入一个数组里面,我们先排序,再查找
学习笔记:
①排序问题,我们在古老的密码中就总结了,这里需要强调排列对象放入普通数组中sort(a,a+n)第一个为首地址,第二个为尾地址,这里切记不要写成a+n-1
如果排列对象放入vector中,我们可以用sort(v.begin(), v.end());
②排序之后可以用lower_bound查找大于或等于x的第一个位置。int p = lower_bound(a,a+n, x) - a;这里切记要-a,当然查找的时候我们也可以用以前的方法直接for循环查找
补充下vector(不定长数组):
①它把一些常见的操作封装在vector类型内,a.size()读取它的大小,a.resize()改变大小,a.push_back()向尾部添加元素,a.pop_back()删除最后一个元素
②vector是一个模板类,所以需要用vector<int> a或者 vector<double> b
③vector<int>是一个类似于int a[]的整数数组,而vector<string>就是一个类似string a[]的字符串数组
④vector看上去是一等公民
===============================================================================
参考代码:
//.cpp#include <iostream>#include <algorithm>const int maxn = 10000;using namespace std;int main(){ int m,n,a[maxn],x,kase = 0; while(cin >> m >> n && m) { cout << "CASE# " << ++kase << ":" << endl; for(int i = 0; i < m; i++) { cin >> a[i]; } sort(a,a+m); while(n--) { cin >> x; int p = lower_bound(a, a+m, x) - a; //在已排序数组a中寻找x if(a[p] == x) cout << x << " found at " << p+1 << endl; else cout << x << " not found " << endl; } } return 0;}
- UVa 10474 Where is the Marble?(大理石在哪儿)
- 大理石在哪儿(Where is the marble?,UVa 10474)
- uva 10474 Where is the Marble?(大理石在哪儿)
- 10474 - 大理石在哪儿Where is the Marble?(排序+检索)
- uva10474大理石在哪儿where is the marble?
- 大理石在哪儿 where is the Marble? lower_bound()
- 大理石在哪儿 where is the Marble?
- 大理石在哪(Where is the Marble?, uva 10474)
- 大理石在哪?(Where is the Marble?, UVa 10474)代码
- 《算法竞赛入门经典2ndEdition 》例题5-1 大理石在哪儿(Where is the marble?, Uva10474)
- 例题5-1 UVA 10474 Where is the Marble?大理石在哪?
- where is the marble?(uva 10474)
- UVa 10474 Where is the Marble?
- Uva 10474 Where is the Marble?
- uva 10474 Where is the Marble?
- UVA 10474 Where is the Marble?
- UVA 10474 - Where is the Marble
- uva-10474 - Where is the Marble?
- 7-2. A+B和C (15)
- IScroll5中文API整理,用法与参考
- centos5.x升级python至python2.7
- OpenMp编程
- 基于curl的异步http实现
- UVa 10474 Where is the Marble?(大理石在哪儿)
- Centos 5.x 升级 python2.7,安装setuptools、mysqldb 完整记录
- Java用dom和sax解析xml代码(Jaxp)
- 7-3. 数素数 (20)
- JSON以及Java转换JSON的方法(前后端常用处理方法)
- python生成器
- nginx中用passenger配置ruby on rails页面
- 国外素材网站
- oracle 创建视图view