UVA 10474 Where is the Marble?
Raju and Meena love-to-play with marbles. They has 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 to 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 that you had to write a program, which would help you in your role as Raju.
Input
There can multiple test cases. Total no of test cases was 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 won't come in any particular order. FollowingQ lines would have Q queries. Be assured, none of the input numbers is greater than 10000 and none of them are negative.
Input is terminated by a test case where N = 0 andQ = 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 would depend upon whether or not the query number was written upon any of the marbles. The different formats is described below:
- 'x found at y', if the first marble with numberx is found at position y. Positions is numbered1, 2,..., N.
- 'x not found', if the marble with numberx was not present.
Look at the output for sample input for details.
Sample Input
4 1235155 213331230 0
Sample Output
case# 1:5 found at 4case# 2:2 not Found3 found at 3
The main topic: Each group of data is divided into two parts, the first part is the original data, the second part is to find the data, to find out in part of the data of the location of the first part of the data.
problem-solving ideas: The first part of the data sorted, and then in the first part of the data directly search the second part of the data is good.
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm>using namespace Std;int Num[10005];int Main () {int m, n, Case = 1;while (scanf ("%d%d", &m, &n) = = 2, M + m) {printf ("case#%d:\n" , case++), for (int i = 0; i < m; i++) {scanf ("%d", &num[i]);} Sort (num, num + m); int A, flag;for (int i = 0; i < n; i++) {flag = 0;scanf ("%d", &a); for (int j = 0; J < m; J + +) {if (num[j] = = a) {printf ("%d found at%d\n", A, J + 1); flag = 1;break;}} if (!flag) printf ("%d not found\n", a);}} return 0;}
UVA 10474 Where is the Marble? (sort)