例题 5-1 STL
Raju and Meena love to play with Marbles. They have got a lot of
marbles with numbers written on them. At the beginning, Raju would
place the marbles one after another in ascending order of the numbers
written on them. Then Meena would ask Raju to find the first marble
with a certain number. She would count ....... Raju gets one point
for correct answer, and Meena gets the point if Raju fails. After some
fixed number of trials the game ends and the player with maximum
points wins. Today it’s your chance to play as Raju. Being the smart
kid, you’d be taking the favor of a computer. But don’t underestimate
Meena, she had written a program to keep track how much time you’re
taking to give all the answers. So now you have to write a program,
which will help you in your role as Raju.
Input
There can be multiple test cases. Total no of test cases is less than . Each test case consists begins
with integers: N the number of marbles and Q the number of queries Mina would make. The next
N lines would contain the numbers written on the N marbles. These marble numbers will not come
in any particular order. Following Q lines will have Q queries. Be assured, none of the input numbers
are greater than and none of them are negative.
Input is terminated by a test case where N = and Q = .
Output
For each test case output the serial number of the case.
For each of the queries, print one line of output. The format of this line will depend upon whether
or not the query number is written upon any of the marbles. The two different formats are described
below:
• ‘x found at y’, if the first marble with number x was found at position y. Positions are numbered
, , . . . , N.
• ‘x not found’, if the marble with number x is not present.
Look at the output for sample input for details.
Sample Input Sample Output
CASE# :
found at
CASE# :
not found
found at
题目的 意思 大概就是 第一行 给你两个数字 第一个 代表了 第二行数字的个数 第二个 代表了 第三行数字的个数 将第二行的由小到大 排序之后 从第三行 中的 数字 和 第二行比较 如果 有相同的 就 打印 出 他排序之后 是第几个数字.
// 关键是 第17行的 标准模板库 里面 的 一点东西 这也算是 第一次 有意识到 接触 标准模板库了吧.
#include<cstdio>
#include<algorithm>
using namespace std;
int main()
{
int q,n,a[],b,c,i,x,p,m,j,kase=;
while(scanf("%d%d",&n,&m)==&&n)
{
printf("CASE# %d\n",++kase);
for(i=;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
while(m--)
{
scanf("%d",&x);
p=lower_bound(a,a+n,x)-a; // 返回的 是 数组的 下标
if(a[p]==x)
printf("%d found at %d\n",x,p+);
else
printf("%d not found\n",x);
}
}
return ;
}
例题 5-1 STL的更多相关文章
- STL学习笔记(不定期更新)
algorithm *1.sort() 用法:sort(数组名,名+长度(,cmp)); int cmp(T a,T b)//T是要排序的a,b的类型,也可以是结构体中任意成员变量 { return ...
- STL模板中的map的使用与例题
最近的计分赛,记得自己的都只是过了两题.遇到了两次map,自己在寒假看了一点的map,只知道在字符串匹配的时候可以用的到.但是自己对map的使用还是不够熟练使用,这回在第一次和第二次的计分赛中都遇到可 ...
- 【C/C++】例题5-4 反片语/算法竞赛入门经典/C++与STL入门/映射:map
本题是映射:map的例题. map:键值对. [题目] 输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一个单词. 在判断是否满足条件时,字母不分大小写,但在输出 ...
- 紫书 例题7-14 UVa 1602(搜索+STL+打表)
这道题想了很久不知道怎么设置状态,怎么拓展,怎么判重, 最后看了这哥们的博客 终于明白了. https://blog.csdn.net/u014800748/article/details/47400 ...
- stl学习(三)crope的用法
转载自http://blog.csdn.net/iamzky/article/details/38348653 曾经我不会写平衡树……于是在STL中乱翻……学到了pb_ds库中的SXBK的斐波那契堆. ...
- 【c++】标准模板库STL入门简介与常见用法
一.STL简介 1.什么是STL STL(Standard Template Library)标准模板库,主要由容器.迭代器.算法.函数对象.内存分配器和适配器六大部分组成.STL已是标准C++的一部 ...
- stl 和并查集应用
抱歉这么久才写出一篇文章,最近进度有点慢.这么慢是有原因的,我在想如何改进能让大家看系列文章的时候更方便一些,现在这个问题有了答案,在以后的推送中,我将尽量把例题和相关知识点在同一天推出,其次在代码分 ...
- 剑指offer例题分享--6
前言:继续整理例题,快速做完这部分,然后继续用C++去刷数据结构和算法的题. 面试题28: 代码如下: #include<iostream> #include<stdio.h> ...
- STL语法——集合:set 安迪的第一个字典(Andy's First Dictionary,UVa 10815)
Description Andy, , has a dream - he wants to produce his very own dictionary. This is not an easy t ...
随机推荐
- Mysql Replace语句的使用
Mysql Replace语句的语法: REPLACE [LOW_PRIORITY | DELAYED] [INTO] tbl_name [(col_name,...)] VALUES ({expr ...
- zset(sorted set:有序集合)数据类型【八】
一.概述:(zset) Sorted-Sets和Sets类型极为相似,它们都是字符串的集合,都不允许重复的成员出现在一个Set中.它们之间的主要差别是Sorted-Sets中的每一个成员都会有一个分数 ...
- hdu - 1068 Girls and Boys (二分图最大独立集+拆点)
http://acm.hdu.edu.cn/showproblem.php?pid=1068 因为没有指定性别,所以要拆点,把i拆分i和i’ 那么U=V-M (M是最大匹配,U最大独立集,V是顶点数) ...
- [bzoj3630][JLOI2014]镜面通道_计算几何_网络流_最小割
镜面通道 bzoj-3630 JLOI-2014 题目大意:题目链接. 注释:略. 想法: 我们发现,只要上下界没有被完全封死,我们就一定有一条合法的光路. 所以只需要将上界和下界拆开即可. 拆点,把 ...
- Mysql五大引擎之间的区别和优劣之分
数据库引擎介绍 MySQL数据库引擎取决于MySQL在安装的时候是如何被编译的.要添加一个新的引擎,就必须重新编译MYSQL.在缺省情况下,MYSQL支持三个引擎:ISAM.MYISAM和HEAP.另 ...
- 解决Spring MVC无法接收AJAX使用PUT与DELETE请求传输的内容
解决Spring MVC无法接收AJAX使用PUT与DELETE请求传输的内容 解决方案 在 Web.xml文件中 加入以下代码 <!--解决ajax Put与Del请求无法接收到传输的内容-- ...
- Java 输入一个正整数,按蛇形打印。
参考博客: http://yangyingming.com/article/371/ //输入一个正整数n(n<=30),输出n所对应的蛇形矩阵.举两个例子: //n=10时,蛇形矩阵为: ...
- php-fpm回顾和总结
时间久了很容易忘,这里做个备份 FastCGI协议php语言的实现,可以高效处理来自web端的动态请求 php-fpm维护一个或者多个php-cgi进程池,处理请求时不需要频繁创建进程 所以比传统的C ...
- mssql存储过程异常处理
MSSQL2000和MSSQL2005以上版本的异常处理语法是不相同的. SQL Server 2005以上版本支持结构化异常处理,而MSSQL2000是不支持的. 1)先看MSSQL 2000的异常 ...
- pycharm内存不足时如何修改设置?
Help->Find Action->(type "VM Options")->(Click)"Edit Custom VM Options" ...