UVa10474 Where is the Marble?(排序sort)
2024-09-05 00:56:14
今天开始学STL,这是书上的一道例题,主要是用了sort函数和lower_bound函数,挺容易理解的。
lower_bound的作用是查找“大于或等于x的第一个位置”。
需要注意的是,不要忘记algorithm头文件。
使用STL真的方便了不少啊!
Where is the Marble?,UVa 10474
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = ; int main() {
int n,q,x,a[maxn],kase=;
while(scanf("%d%d",&n,&q)==&&n){
printf("CASE# %d:\n",++kase);
for(int i=;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;//在已排序数组a中寻找x
if(a[p]==x) printf("%d found at %d\n",x,p+);
else printf("%d not found\n",x);
}
}
return ;
}
最新文章
- Can&#39;t use Subversion command line client: svn Probably the path to Subversion executable is wrong. Fix it.
- VS2012 InstallShield2013LimitedEdition激活
- $(";#id";).val()取值textarea是";";
- UVA 11827	Maximum GCD
- oracle11g rac asm存储数据迁移
- iOS中生成并导入基于Swift编程语言的Framework
- Swift -- 官方文档Swift-Guides的学习笔记
- C语言中结构体的初始化
- Ubuntu环境下Hadoop1.2.1, HBase0.94.25, nutch2.2.1各个配置文件一览
- 广播接收者 BroadcastReceiver 示例-2
- jsp中包含JAVA代码
- effective c++ 条款5 c++ 默默实现的函数
- 【Python 函数对象 命名空间与作用域 闭包函数 装饰器 迭代器 内置函数】
- BZOJ_4378_[POI2015]Logistyka_树状数组
- LaTeX教程与下载
- CodeIgniter中使用base_url()时显示http://::1/ci/
- wpf 的依赖属性只能在loaded 事件之后才能取到
- Redis之序列化POJO
- Android pm 命令详解
- Poi中getPhysicalNumberOfCells 与 getLastCellNum的差异