本文主要是介绍uva 10474 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 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
lucky 猫的中文翻译:
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的彈珠
輸出格式請參考Sample Output。
#include <bits/stdc++.h>
using namespace std;
//fstream in,out;
vector<int> vi;
int main()
{ios::sync_with_stdio(false);int n,m,k=1;while(cin>>n>>m,n+m){vi.clear();for(int i=0;i<n;i++){int t;cin>>t;vi.push_back(t);}sort(vi.begin(),vi.end());cout<<"CASE# "<<k++<<':'<<endl;for(int i=1;i<=m;i++){int t;cin>>t;auto x=lower_bound(vi.begin(),vi.end(),t);if(x==vi.end()||*x!=t){cout<<t<<" not found"<<endl;}elsecout<<t<<" found at "<<x-vi.begin()+1<<endl;}}return 0;
}
解答:
超级简单题,用二分查找lower_bound可以过,用find也可以过。
这篇关于uva 10474 Where is the Marble?的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!