UVa 10474 - Where is the Marble?

Where is the Marble? 

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 nextN 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 numbered1, 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



题意:

Raju和Meena喜歡玩彈珠,他們有許多上面有號碼的彈珠。一開始時,Raju按照彈珠上面的號碼由小到大排成一列,然後Meena會要求Raju找出某個號碼的第一顆彈珠所在的位置。她會算1...2...3...,如果Raju答對了,他就得1分,否則Meena得1分。玩了多次之後誰的得分多誰就贏了。今天你有機會扮演Raju的角色。由於你們都是很聰明的小孩,你會用電腦來計算,而Meena則寫了一個程式來檢查你花多少時間來回答所有的問題。

Input

輸入含有多組測試資料,每組測試資料的第一列有2個正整數N、Q,N代表彈珠的數目,Q代表對於此組測試資料Meena問的問題的數目。接下來的N列每列有一個整數,代表這N個彈珠上的號碼(未經排序)。在接下來的Q列每列有一個整數代表Meena所問的問題(球的號碼)。所有輸入的數字都不會大於10000,且沒有負的。

當N=0, Q=0時代表輸入結束。請參考Sample Input。

Output

對每組測試資料請先輸出一列,這是第幾組測試資料。對每組測試資料Meena所問的每個問題輸出一列,輸出格式如下其中之一:

  • x found at y      如果第一個號碼為x的彈珠在位置y被發現(位置從1開始算)
  • x not found      如果找不到號碼為x的彈珠

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;

const int MAX = 10000+5;

int n,q;
int num[MAX];

int cmp(const void *a, const void *b)
{
	return *(int *)a-*(int *)b;
}

int main()
{
//	freopen("in.txt","r",stdin);
	int nCase=1;
	while(cin>>n>>q)
	{
		if(n==0 && q==0) break;
		
		memset(num, 0, sizeof(num));
		int max=0;
		for(int i=0; i < n; i++)
		{	
			cin>>num[i];
			if(num[i]>max)
				max= num[i];
		}
		if(n>max) max=n;
		// 没注意这个冒号 贡献了一次WA 
		cout << "CASE# " << nCase++ << ":"<< endl;
		qsort(num, n, sizeof(num[0]), cmp);
		int pos;
		for(int j=0; j < q; j++)
		{
			cin>>pos;
			int length=0;
			int k=0;
			for( ;k < max; k++)
			{
				if(num[k]==pos)
				{
					length++;
					break;
				}else length++;
			}
			if(k==max) cout << pos << " not found" << endl;
			else
				cout << pos <<" found at " << length << endl;				
		}
	}
	return 0;
} 


你可能感兴趣的:(C++,算法,uva)