UVa10474

Where is the Marble?

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status Practice UVA 10474 uDebug

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

题意:

输入n和m,之后输入n个数,然后又输入m个数。输出这m个数,每个在n个数由小到大顺序中排在第几位。

输入:

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.

输出:

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.

分析:

Vector数组可以认为是大小的数组,它可以实现排序与查找功能,函数分别是sort(v.begin(),v.end() 和lower_bound(v.begin(),v.end(),x)。如果查找返回的是指针v.end()或者发现*it!=x那么容器里没有x这个数。

 1 #include <cstdio>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <vector>
 5 #include <string>
 6 #include <algorithm>
 7 #include <cmath>
 8 using namespace std;
 9 int n,m;
10 int main(){
11     vector<int> v;
12     int Case = 0;
13     while(scanf("%d%d",&n,&m) != EOF &&(n || m)){
14         v.clear();
15         int x;
16         for(int i = 0 ; i < n ; i++){
17             scanf("%d",&x);
18             v.push_back(x);
19         }
20         sort(v.begin(),v.end());
21         printf("CASE# %d:\n",++Case);
22         for(int i = 0 ; i < m ; i++){
23             scanf("%d",&x);
24             vector<int>::iterator it = lower_bound(v.begin(),v.end(),x);
25             if(it == v.end())
26                 printf("%d not found\n",x);
27             else{
28                 if(*it == x)
29                     printf("%d found at %d\n",x,it - v.begin() + 1);
30                 else
31                     printf("%d not found\n",x);
32             }
33         }
34     }
35     return 0;
36 }

时间: 2024-12-13 17:36:56

UVa10474的相关文章

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?

UVA10474 大理石在哪儿 Where is the Marble? 题意翻译 现有N个大理石,每个大理石上写了一个非负整数.首先把各数从小到大排序,然后回 答Q个问题.每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上 写着x.排序后的大理石从左到右编号为1-N. 输入输出样例: //输入 4 1 4个大理石 1个问题 2 2 3 5 1 大理石上写的数字 3 5 1 5 这个5是问题 是否有一个大理石写着5 5 2 1 3 3 3 1 2 3 0 0 //输出 CAS

uva10474大理石在哪儿where is the marble?

背景:做了这么久的题,唯一一道一次ac的,可见这道题是如何的简单. 思路:思路很清楚的模拟题,先排序再查找. 学习:sort函数和lower_bound函数,sort函数排序就不多说了,lower_bound函数作用是查找一个数组中大于等于x的第一个位置. #include <iostream> #include <stdio.h> #include <algorithm> using namespace std; int figue[10000]; void prin

排序与检索【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 the

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

大理石在哪儿(UVa10474)

题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=835&page=show_problem&problem=1415 C++ 11代码如下: 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 int num[10000]; 5 int mai

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"

大理石在哪儿 UVa10474

现有N个大理石,每个大理石写了一个非负整数.首先把各数从小到大排序,然后回答Q个问题.每个问题是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上写着x.排序后的大理石从左到右编号为1~N.(在样例中,为了节约篇幅,所有大理石上的数合并到一行,所有问题也合并到一行.) 样例输入: 4 1 2 3 5 1 5 5 2 1 3 3 3 1 2 3 样例输出: CASE# 1: 5 found at 4 CASE# 2: 2 not found 3 found at 3 //UVa 10474

10道ACM例题让你三天学会STL

清明节给大一的孩子们讲了点STL的应用,下了些功夫,搬到这里来供大家学习交流. 1.泛型程序设计简介与迭代器的介绍 2.常见的STL容器及其例题应用(UVA10474,UVA101,UVA10815,UVA156,UVA540,UVA136 HDU1027,CF501B,HDU1716,HDU4277) 3.相关练习和思路 1.泛型程序设计简介与迭代器的介绍 1.1 泛型程序设计简介 泛型程序设计,简单地说就是使用模板的程序设计法.将一些常用的数据结构(比如链表,数组,二叉树)和算法(比如排序,