Where is the Marble?

 Description

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 1...2...3. 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 65. Each test case consists begins with 2 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 10000 and none of them are negative.

Input is terminated by a test case where N = 0 and Q = 0.

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 1, 2,..., N.
`x not found', if the marble with number x is not present.
Look at the output for sample input for details.

Sample Input
4 1
2
3
5
1
5
5 2
1
3
3
3
1
2
3
0 0
Sample Output
CASE# 1:
5 found at 4
CASE# 2:
2 not found

3 found at 3
分析:这个题目是算法竞赛上的一个题目,书中给力详细的解释

 # include <cstdio>
 # include <algorithm>

 using namespace std;
 ;
 int main3()
 {
     ;
     int x;
      && n)
     {
         printf("The NO.%d\n", ++kase);
         ; i < n; i++)
         {
             scanf("%d", &a[i]);
         }
             sort(a, a + n);//排序
             while (q--)//多组输入
             {
                 scanf("%d", &x);//取决于q
                 int p = lower_bound(a, a + n, x)-a ;      //只有指向同一数组的俩个指针变量之间才可以进行计算。否则是没有意义的。
两指针变量相减是两指针变量相减所得之差,是俩个指针所指数组之间相差的元素个数。实际上是俩个指针值(地址)相减之差再除以该数组元素的长度(字节数),
注意:因为俩个指针相加没有任何意义,所以别乱搞。 if (a[p] == x) printf(); else printf("%d not found\n", x); } } ; }

//说明:
关于第21行lower_bound(p,p+n,x)作用是在p[n]数组中查找大于或等于x的第一个位置,得到的结果是一个指针,前提是数组p[n]进行了排序;
指向同一个数组的两个指针相减,结果为两个指针之间的元素数目;
即p[n]存放的第一个数到x这个数之间的元数个数。
这东西有个学名叫迭代器。

排序与检索【UVa10474】Where is the Marble?的更多相关文章

  1. UVa10474 Where is the Marble?(排序sort)

    今天开始学STL,这是书上的一道例题,主要是用了sort函数和lower_bound函数,挺容易理解的. lower_bound的作用是查找“大于或等于x的第一个位置”. 需要注意的是,不要忘记alg ...

  2. UVA10474 Where is the Marble?【排序】

    参考:https://blog.csdn.net/q547550831/article/details/51326321 #include <iostream> #include < ...

  3. C#中DataTable排序、检索、合并等操作实例

    转载引用至:http://www.jb51.net/article/49222.htm     一.排序1.获取DataTable的默认视图2.对视图设置排序表达式3.用排序后的视图导出的新DataT ...

  4. DataTable数据进行排序、检索、合并、分页、统计

    在做程序时经常遇到要将反复对数据进行筛选.求和.排序.分页等的情况.每次的数据操作都要去访问数据库很明显是不合理的!当然需要实时数据的情况除外,不做讨论哈.今天无意间在网上看到了这篇文章,挺实用的,拿 ...

  5. STL排序和检索

    //参考书是刘汝佳的那本算法书P108 //sort的用法也就是本来是从小到大排序,如果想要从大到小,中间写一个比较函数就可以了: //以下两个检索的东西 //lower_bound找到一个值的最小插 ...

  6. Uva10474 - Where is the Marble?

      两种解法: 1.计数排序 //计数排序 #include<cstdio> #include<iostream> #include<vector> #includ ...

  7. STL之 sort排序与检索(2019.1.18)

    1.作用对象:数组  a[n] 头文件:#include<algorithm> 内容:sort(a,a+n) 功能:进行升序排序 内容:lower_bound(a,a+n,x) 功能:找到 ...

  8. 【OI】C++STL初步 排序与检索

    从紫皮书过来的,但是书中内容讲的比较简洁,做一点补充笔记. 一.排序(sort函数) 头文件:<algorithm> 语法:sort(start,end,cmp); start,end必须 ...

  9. Asp.Net MVC 分页、检索、排序整体实现

    很多时候需要这样的功能,对表格进行分页.排序和检索.这个有很多实现的方式,有现成的表格控件.用前端的mvvm,用户控件.但很多时候看着很漂亮的东西你想进一步控制的时候却不那么如意.这里自己实现一次,功 ...

随机推荐

  1. OpenGL 的空间变换(上):矩阵在空间几何中的应用

    在使用 OpenGL 的应用程序中,当我们指定了模型的顶点后,顶点依次会变换到不同的 OpenGL 空间中,最后才会被显示到屏幕上.在变换的过程中,通过使用矩阵,我们更高效地来完成这些变换工作. 本篇 ...

  2. java集合的核心知识

    1.    集合 1.1. 什么是集合 存储对象的容器,面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,存储对象,集合是存储对象最常用的一种方式. 集合的出现就是为了持有对象. ...

  3. [内存管理]连续内存分配器(CMA)概述

    作者:Younger Liu, 本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 未本地化版本许可协议进行许可. 原文地址:http://lwn.net/Articles/396657/ 1 ...

  4. Laravel 中使用 Redis 数据库

    一.前言 Redis 是一个开源高效的键值对存储系统,它通常用作为一个数据结构服务器来存储键值对,它可以支持字符串.散列.列表.集合.有序集合. 1. 安装 predis/predis 在 Larav ...

  5. 【lucene系列学习四】log4j日志文件实现多线程的测试

    参考资料:http://nudtgk2000.iteye.com/blog/1716379 首先,在http://www.apache.org/dyn/closer.cgi/logging/log4j ...

  6. 【NLP】3000篇搜狐新闻语料数据预处理器的python实现

    3000篇搜狐新闻语料数据预处理器的python实现 白宁超 2017年5月5日17:20:04 摘要: 关于自然语言处理模型训练亦或是数据挖掘.文本处理等等,均离不开数据清洗,数据预处理的工作.这里 ...

  7. OD常用断点之CC断点

    在做Windows平台软件逆向时,Ollydbg是极其常用的逆向工具,动态调试功能非常强大.在调试过程中,断点的使用有助于确定关键的破解位置,今天讲解的内容是OD常用断点中的CC断点. CC断点有很多 ...

  8. javascript的面向对象详解

    每次说到javascript到面向对象,总感觉自己心里懂,但是却不知道该怎么说,这就是似懂非懂到表现,于是乎,每次一说,就要到处去查找资料,零零碎碎到看了一些,感觉有懂了,但是过段时间,好像又不知道是 ...

  9. android webview和 javascript 进行交互

    HTML5进行app开发具有开发快,跨平台等优点,但是当客户需要访问照相机或者调用摄像头等硬件的时候,H5就会有限制,必须要调用原生方法进行设备访问.下面简要介绍JS和原生方法互相调用的方法: 1 在 ...

  10. php 中时间函数date及常用的时间计算

    曾在项目中需要使用到今天,昨天,本周,本月,本季度,今年,上周上月,上季度等等时间戳,趁最近时间比较充足,因此计划对php的相关时间知识点进行总结学习 1,阅读php手册date函数 常用时间函数: ...