排序与检索【UVa10474】Where is the Marble?

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
分析:这个题目是算法竞赛上的一个题目,书中给力详细的解释

 1 # include <cstdio>
 2 # include <algorithm>
 3
 4 using namespace std;
 5 const int maxn = 10000;
 6 int main3()
 7 {
 8     int n, q,a[maxn],kase = 0;
 9     int x;
10     while (scanf("%d %d", &n, &q) == 2 && n)
11     {
12         printf("The NO.%d\n", ++kase);
13         for (int i = 0; i < n; i++)
14         {
15             scanf("%d", &a[i]);
16         }
17             sort(a, a + n);//排序
18             while (q--)//多组输入
19             {
20                 scanf("%d", &x);//取决于q
21                 int p = lower_bound(a, a + n, x)-a ;//只有指向同一数组的俩个指针变量之间才可以进行计算。否则是没有意义的。                                                             两指针变量相减是两指针变量相减所得之差,是俩个指针所指数组之间相差的元素个数。实际上是俩个指针值(地址)相减之差再除以该数组元素的长度(字节数),                                                             注意:因为俩个指针相加没有任何意义,所以别乱搞。
22                 if (a[p] == x)
23                     printf("%d found at %d\n", x, p + 1);
24                 else
25                     printf("%d not found\n", x);
26
27
28             }
29
30     }
31     return 0;
32 }

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

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

UVA10474 Where is the Marble?

问题链接:UVA10474 Where is the Marble?. 题意简述:输入n个整数,代表大理石编号:再输入q个数(编号),问是否有这个编号的大理石,位置在哪里? 这个问题用C++语言编写程序,主要是为了练习使用STL的功能. 程序中,使用了算法库(algorithm)中的两个函数:使用sort()函数用于对数据排序,该函数的参数比C语言的同类函数简单,程序更加易于书写:使用函数lower_bound()查找元素,简单方便. AC的C++语言程序如下: /* UVA10474 Wher

UVA10474 Where is the Marble?【排序】

参考:https://blog.csdn.net/q547550831/article/details/51326321 1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 #define N 10000 6 int main() 7 { 8 int n,q,count=0; 9 10 while (scanf("%d %d"

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

在做程序时经常遇到要将反复对数据进行筛选.求和.排序.分页等的情况.每次的数据操作都要去访问数据库很明显是不合理的!当然需要实时数据的情况除外,不做讨论哈.今天无意间在网上看到了这篇文章,挺实用的,拿来记忆一下 一.排序1 获取DataTable的默认视图2 对视图设置排序表达式3 用排序后的视图导出的新DataTable替换就DataTable(Asc升序可省略,多列排序用","隔开)DataView dv = dt.DefaultView;dv.Sort = "id As

第五章 排序和检索数据

1.排序数据: select column_name_0 from table_name order by column_name_1; 2.按多个列排序: 3.指定排序方向: 数据排序默认是升序排序,使用关键字desc可以进行降序排序 注意点:desc关键字只应用到直接位于其前面的列名. 与desc对应的是asc.使用asc或者不使用desc的时候是按照升序排列. 4.在对文本性数据进行排序的时候,mysql会将大写字母与其对应的小写字母视为等价的. 5.使用order by 和limit的组

UVa10474 Where is the Marble ? 有序数组二分找值 lower_bound / upper_bound

题意: 给出n个数,先把各数从小到大排序,然后q次询问xi在数组中的位置,不存在则输出相应信息. 输入样例: 4 1 2 3 5 1 5 5 2 1 3 3 3 1 2 3 0 0 输出样例: CASE# 1: 5 found at 4 CASE# 2: 2 not found 3 found at 3 //======================================= 数组从小到大有序. lower_bound :查找"大于或者等于x"的第一个位置. upper_bo

算法入门系列之排序与检索

UVA340   UVA10420 时间有点久远,很早之前写的,然后忘记总结了,这道题其实很容易,一行只取第一个字符串,然后按照字典序输出每个字符串的个数. 这里有个技巧就是先用scanf获取第一个字符串,然后再用gets直接吸收剩下的字母.其次就是用map记录个数,然后用迭代器输出结果 #include<iostream> #include<cstring> #include<cstdio> #include<map> using namespace st

PAT A 1022. Digital Library (30)【结构体排序检索】

https://www.patest.cn/contests/pat-a-practise/1022 直接模拟, 输入,按id排序,检索 #include <iostream> #include <string> #include <algorithm> using namespace std; struct book //图书结构 { string id; string title; string author; int k; //关键词数量 string key[5

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

原文:Asp.Net MVC 分页.检索.排序整体实现 很多时候需要这样的功能,对表格进行分页.排序和检索.这个有很多实现的方式,有现成的表格控件.用前端的mvvm,用户控件.但很多时候看着很漂亮的东西你想进一步控制的时候却不那么如意.这里自己实现一次,功能不是高大全,但求一个清楚明白,也欢迎园友拍砖.前端是bootstrap3+jPaginate,后台基于membership.没什么难点. 先上效果图. 分页其实就是处理好 每页项目数.总项目数.总页数.当前页.为了方便复用,就先从仓库开始说起

javascript数据结构与算法--基本排序算法分析

javascript中的基本排序算法 对计算机中存储的数据执行的两种最常见操作是排序和检索,排序和检索算法对于前端开发尤其重要,对此我会对这两种算法做深入的研究,而不会和书上一样只是会贴代码而已,下面我会一步步从自己的理解的思路来一步步学习各个排序的思想.不过这些算法依赖于javascript中的数组来存储数据.最后我会来测试下基本算法(冒泡排序,选择排序,插入排序)的那个效率更高! 下面啊,我们先可以来封装常规数组操作的函数,比如:插入新数据,显示数组数据,还有交换数组元素等操作来调用不同的排