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. 捕获mssqlservice 修改表后的数据,统一存储到特定的表中,之后通过代码同步两个库的数据

    根据之前的一些想法,如果有A,B 两个数据库, 如果把A 用户通过界面产生的更新或者插入修改,操作的数据同步更新到B 库中,如果允许延时2分钟以内 想法一: 通过创建触发器 把变更的数据和对应的表名称 ...

  2. 浅析c++/java/c#三大热门编程语言的运行效率

    从安全角度考虑,C#是这几中语言中最为安全的,它其中定义的相关安全机制很好的确保了系统的安全... 今天和同学们一起探讨下c++/java/c# 三大热门语言的运行效率情况,以及各自的用途. 估计有很 ...

  3. zabbix主动上报的python脚本

    本代码linux.window平台通用. 不过我遇到一个Windows2008缺少运行库的提示,可能这个是Python3.6开发的.需要安装vc2015运行库吧.我没调试成功. linux平台还可以监 ...

  4. C/C++对bool operator < (const p &a)const的认识,运算符重载详解(杂谈)

    下面来进行这段代码的分析: struct node {  //定义一个结构体node(节点)    int x;    int y;    int len;   //node中有3个成员变量x,y,l ...

  5. J2EE struts2MVC应用在线书签1

    序:之前花了一天研究了一下filter,虽然是实现了MVC模式开发了 WebBookmark,但是代码过于冗长,集中在filter中使用if语句不易阅读,为了体现两份作业的不同点,我决定学习 Java ...

  6. object detection技术演进:RCNN、Fast RCNN、Faster RCNN

    object detection我的理解,就是在给定的图片中精确找到物体所在位置,并标注出物体的类别.object detection要解决的问题就是物体在哪里,是什么这整个流程的问题.然而,这个问题 ...

  7. 解决NSTimer循环引用Retain Cycle问题

    解决NSTimer循环引用Retain Cycle问题 iOS开发中以下的情况会产生循环引用 block delegate NSTimer 循环引用导致一些对象无法销毁,一定的情况下会对我们横须造成影 ...

  8. PHP三维数组拼装

    foreach ($records as $k => $v) { foreach ($records as $m => $n) { if ($v['days'] == $n['days'] ...

  9. __builtin_popcount(n)

    Gcc提供的内建函数__builtin_popcount(n),可以精确计算n表示成二进制时有多少个1.借助这个函数可以快速判断一个数是否是2的幂. bool isPowerOfTwo(int n) ...

  10. poj2774 Long Long Message 后缀数组求最长公共子串

    题目链接:http://poj.org/problem?id=2774 这是一道很好的后缀数组的入门题目 题意:给你两个字符串,然后求这两个的字符串的最长连续的公共子串 一般用后缀数组解决的两个字符串 ...