传送门:

UVa10474 - 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

分析:

现有N个大理石,每个大理石上写了一个非负整数。首先把各数从小到大排序,然后回答Q个问题。每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上写着x。排序后的大理石从左到右编号为1~N。

code:

#include<iostream>
#include<algorithm>
#include<stdio.h>
using namespace std;
int main()
{
int n,q,k=;
while(cin>>n>>q)
{
//n个大理石 q的问题 x猜测数字
if(n==&&q==)
break;
printf("CASE# %d:\n",k++);
int a[n];
for(int i=;i<n;i++)
{
cin>>a[i];
}
int x;
sort(a,a+n);//排序
while(q--)
{
cin>>x;
int i=lower_bound(a,a+n,x)-a;//二分查找,查找大于或等于x的第一个位置
if(a[i]==x)
{
printf("%d found at %d\n",x,i+);
}else
{
printf("%d not found\n",x);
}
}
}
return ;
}

10474 - Where is the Marble?(模拟)的更多相关文章

  1. UVA.10474 Where is the Marble ( 排序 二分查找 )

    UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了 ...

  2. UVa 10474 Where is the Marble

    题意:给出一列数,先排序,再查找学习了sort函数,lower_bound函数sort:可以给任意对象排序(包括自己定义的)(前提是定义好了‘<’运算符)lower_bound:查找大于或者等于 ...

  3. uva 10474 Where is the Marble? 计数排序

    题目给出一系列数字,然后问哪个数字是从小到大排在第几的,重复出现算第一个. 数据范围为10000,不大,完全可以暴力,sort不会超时. 但是由于以前做比赛时也遇到这种题目,没注意看数据范围,然后暴力 ...

  4. UVA 10474 - Where is the Marble?--vector

    https://vjudge.net/problem/UVA-10474 https://blog.csdn.net/xiyaozhe/article/details/81081344 简单用法 so ...

  5. uvaoj 10474 - Where is the Marble?(sort+lower_bound)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  6. uva 10474 Where is the Marble?(简单题)

    我非常奇怪为什么要把它归类到回溯上,明明就是简单排序,查找就OK了.wa了两次,我还非常不解的怀疑了为什么会 wa,原来是我居然把要找的数字也排序了,当时仅仅是想着能快一点查找.所以就给他排序了,没考 ...

  7. Codeforces Round #175 (Div. 2)

    A. Slightly Decreasing Permutations 后\(k\)个倒序放前面,前\(n-k\)个顺序放后面. B. Find Marble 模拟. C. Building Perm ...

  8. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  9. 刘汝佳 算法竞赛-入门经典 第二部分 算法篇 第五章 3(Sorting/Searching)

    第一题:340 - Master-Mind Hints UVA:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Item ...

随机推荐

  1. opensuse 安装oracle 界面乱码

    执行.runInstall时,出现界面乱码 export LANG=en_US export LC_ALL=en_US 终端里 执行这两句.用英文界面安装 再执行 .runInstall

  2. PlayMaker入门介绍

    http://www.jianshu.com/p/ce791bef66bb   PlayMaker是什么? PlayMaker是Unity3D的一款 可视化 的 有限元状态机(Finite-state ...

  3. Js常用的设计模式(1)——单例模式

    <Practical Common Lisp>的作者 Peter Seibel 曾说,如果你需要一种模式,那一定是哪里出了问题.他所说的问题是指因为语言的天生缺陷,不得不去寻求和总结一种通 ...

  4. Unity 基础

    Unity 基础是unity入门的关键.他将讲解Unity的界面, 菜单项,使用资源,创设场景,并发布版本. 当你读完这段,你将理解unity是怎么工作的,如何有效地使用它,并且完成一个基本的游戏. ...

  5. ASP.NET复习笔记(1)

    今天是个好日子,伴着早上八点的朝阳,我背上书包,提上电脑,带上一根网线,风风火火的冲向教室,因为,我终于想好博客内容写啥了——这不是快期末了么,我就写复习,虽然有些课程还没讲完,但并不影响我做复习,正 ...

  6. LINQ和.NET数据访问

    .NET数据访问 在.NET中对于数据的访问大致有三个层面,数据访问层.内存数据集.业务逻辑层.数据层,包括了XML配置文件以及一些常用的数据库(使用SQL语句):内存数据集,主要是DataSet数据 ...

  7. MVC类库视图在UI使用方法

    在类库中固定文件:Controller Views  引用一下两个程序集(类库和UI项目都需要引用)(似乎只需要引用mvc的,build似乎在引用文件中不见了?有空测试一下) 如此方式,一个登录需要是 ...

  8. Redis的Lists数据类型

    Lists 就是链表,相信略有数据结构知识的人都应该能理解其结构.使用Lists结构,我们可以轻松地实现最新消息排行等功能.Lists的另一个应用就是消息队列,可以利用Lists的PUSH操作,将任务 ...

  9. node.js-cancelled because Node.js is unresponsive

    今天初学node.js,但是在使用vs code 进行启动调试的时候出现了一个问题 这个报错,一开始我并不知道是什么意思.(而截至我写这个笔记我也还没了解清楚) 大概翻译出来的意思是说 “node.j ...

  10. 使用canvas及js简单生成验证码方法

    在很多时候都需要用到验证码,前端验证码需要知道Html5中的canvas知识点.验证码生成步骤是:1.生成一张画布canvas 2.生成随机数验证码  3.在画布中生成干扰线  4.把验证码文本填充到 ...