Where is the Marble?
2016-04-30 21:10
225 查看
Raju and Meena love to play with Marbles. They have got a lot ofmarbles with numbers written on them. At the beginning, Raju wouldplace the marbles one after another in ascending order of the numberswritten
on them. Then Meena would ask Raju to find the first marblewith a certain number. She would count 1...2...3. Raju gets one pointfor correct answer, and Meena gets the point if Raju fails. After somefixed number of trials the game ends and the player with maximumpoints
wins. Today it’s your chance to play as Raju. Being the smartkid, you’d be taking the favor of a computer. But don’t underestimateMeena, she had written a program to keep track how much time you’retaking 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 beginswith 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 comein any particular order. Following Q lines
will have Q queries. Be assured, none of the input numbersare 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 describedbelow:
• ‘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
考察内容:排序和查找
代码:
on them. Then Meena would ask Raju to find the first marblewith a certain number. She would count 1...2...3. Raju gets one pointfor correct answer, and Meena gets the point if Raju fails. After somefixed number of trials the game ends and the player with maximumpoints
wins. Today it’s your chance to play as Raju. Being the smartkid, you’d be taking the favor of a computer. But don’t underestimateMeena, she had written a program to keep track how much time you’retaking 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 beginswith 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 comein any particular order. Following Q lines
will have Q queries. Be assured, none of the input numbersare 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 describedbelow:
• ‘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
考察内容:排序和查找
代码:
#include <iostream> #include <vector> #include <cstdio> #include <algorithm> using namespace std; int main(int argc, const char * argv[]) { int N, Q; int T = 1; while (scanf("%d %d", &N, &Q) && N && Q) { vector<int> v; //定义vector容器,存储N个石头 for (int i = 0; i < N; i++) { int temp; scanf("%d", &temp); //把石头对应的标号插入容器尾部 v.push_back(temp); } //对容器内数据进行排序(默认从小到大的顺序排序) sort(v.begin(), v.end()); printf("CASE# %d:\n", T++); for (int i = 0; i < Q; i++) { int temp; scanf("%d", &temp); int j = 0; // for (j = 0; j < v.size(); j++) { if (v[j] == temp) { //在容器内找到对应的数并输出 printf("%d found at %d\n", v[j], j + 1); break; //必须要添加break语句,因为题目只要求找到第一次出现的位置,不添加break语句,如果容器中有很个的话就会输出多个位置 } } if (j == v.size()) { //容器中不存在,输出找不到 printf("%d not found\n", temp); } } } return 0; }
相关文章推荐
- Android广播机制
- 全文索引----配置solr数据源
- [可并堆] BZOJ 2809 [Apio2012]dispatching
- 静态成员
- 团队冲刺第一阶段第八天
- 转 作业调度算法
- HDU 1814 2-SAT
- Java单元测试工具:JUnit4——JUnit详解之运行流程及常用注解
- Android之观察者模式源码分析(DataSetObserver)
- 1050. 螺旋矩阵(25)
- Boost log 简单使用
- 进程间通信的方式及比较
- 重载、覆盖、重写(隐藏)
- 机电大作业第一次设计文档
- Java重拾之猜数字小游戏
- boost.log要点笔记
- 《Nodejs开发加密货币》之一:开栏的话
- 20145330《Java程序设计》第四次实验报告
- 亿级Web系统的容错性建设实践
- 设计模式之策略模式