UVa 10474 Where is the Marble? 【STL】【排序与检索-sort和lower_bound】
来源:互联网 发布:少女前线 妖精数据 编辑:程序博客网 时间:2024/06/05 04:27
题目链接:点击打开链接
题意:N个大理石,每个大理石上都有一个非负整数。首先按照从小到大顺序排序,然后回答Q个问题,每个问题会有一个正整数x,如果大理石上存在这个数x,则输出他的排好序后的位置,否则输出未找到x。
思路:输入完成后sort排序,然后用lower_bound找出第一个大于或等于x的位置,再判断这个位置上的数是否等于x。
AC代码:
#include <cstdio>#include <iostream>#include <cstring>#include <algorithm>#include <cmath>using namespace std;int a[10005];int main(){ int m, n, tem; int cou = 0; while(~scanf("%d%d",&m,&n)) { if(m == 0 && n == 0) break; memset(a,0,sizeof(a)); for(int i = 0; i < m; ++i) scanf("%d",&a[i]); printf("CASE# %d:\n",++cou); sort(a, a+m); while(n--) { scanf("%d",&tem); int q = lower_bound(a, a+m, tem) - a; //在已排序的数组a中寻找x if(a[q] == tem) cout << tem << " found at " << q + 1 << endl; else cout << tem << " not found" << endl; } } return 0;}
主要是为了练习lower_bound的用法,格式: int tem = lower_bound(数组名, 数组名+查找范围, 查找的元素) - 数组名;
0 0
- UVa 10474 Where is the Marble? 【STL】【排序与检索-sort和lower_bound】
- Uva 10474 Where is the Marble?(排序与检索)
- UVa 10474 - Where is the Marble?【排序和检索】
- UVA 10474 Where is the Marble(stl,sort)
- UVA 10474 (暑假-排序、检索 -C - Where is the Marble?)
- 排序检索-10474 - Where is the Marble?
- 经典第五章例5.1 UVa 10474 Where is the Marble?(排序与检索)
- UVA 10474 Where is the Marble? (STL)
- UVa - 10474 Where is the Marble?(STL)
- UVA 10474 Where is the Marble?(简单排序查找)(附lower_bound改写)
- 排序检索C - Where is the Marble?
- UVA 10474 Where is the Marble?【vector的sort用法】
- 10474 - 大理石在哪儿Where is the Marble?(排序+检索)
- UVA10474 Where is the Marble?(二分查找,STL-lower_bound)
- uva 10474 Where is the Marble?(二分STL)
- UVA 10474:Where is the Marble?(STL初步)
- UVA - 10474 Where is the Marble?(STL,水)
- UVA 10474 Where is the Marble? 暴力|STL
- Java反射
- 清除缓存的封装 menglong0329
- 在 Linux 上配置一个 syslog 服务器
- Perl调用shell命令方法小结(system/反引号/exec)
- 每天一题LeetCode[第三天]
- UVa 10474 Where is the Marble? 【STL】【排序与检索-sort和lower_bound】
- 多线程与多进程通信方式的区别
- mysql索引优化
- Wine 2.0 发布!在 Linux 中运行 Microsoft Office 2013 吧
- php写一个冒泡排序
- Spring 杂谈
- 2016数据库技术盘点
- 电子稳像
- 四个月的蜕变之JDBC的四个步骤和代码实现