传送门:

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. 如何取得GridView被隐藏列的值

    如何取得GridView被隐藏列的值         分类:             ASP.net              2009-06-25 12:47     943人阅读     评论(1 ...

  2. ubuntu 14.04 64bit 安装 oracle 11g r2

    参考文章:http://tutorialforlinux.com/2016/03/09/how-to-install-oracle-11g-r2-database-on-ubuntu-14-04-tr ...

  3. git rebase 和 git merge 总结

    git merge 和 git rebase 都是用于合并分支,但二者是存在区别的. 在使用时,记住以下两点: 当你从 remote 去 pull 的时候,永远使用 rebase(除了一个例外) 当你 ...

  4. mockito 初识

    转载:http://blog.csdn.net/zhoudaxia/article/details/33056093 在平时的开发工作中,经常会碰到开发进度不一致,导致你要调用的接口还没好,此时又需要 ...

  5. 1e6等于多少?

    如果抽象成这样:aeb 要求a不能不写,也就是说是1也要写上 b必须是整数. 实现上就是 a*10^b a乘以10的b次方 所以楼主的就是1*10^6 100000

  6. 01_Quartz基础结构

    [各种任务调度的使用场景] 论坛每天凌晨统计论坛用户的积分排名. 论坛每半个小时生成精华文章. 每隔30分钟对锁定过期的用户解锁. 每月1号统计上个月各部门的业务数据. [Quartz 简介] Qua ...

  7. Flowchart

    1. 工具可使用 https://www.processon.com 2.

  8. unity获取相机视窗口大小

    using UnityEngine; using System.Collections; public class CameraView : MonoBehaviour { private Camer ...

  9. HTML基础内容(持续更新...)

    1.<!DOCTYPE html>声明有助于浏览器中正确显示网页 HTML5<!DOCTYPE html>HTML 4.01<!DOCTYPE HTML PUBLIC & ...

  10. 对 Canal (增量数据订阅与消费)的理解

    概述 canal是阿里巴巴旗下的一款开源项目,纯Java开发.基于数据库增量日志解析,提供增量数据订阅&消费,目前主要支持了MySQL(也支持mariaDB). 起源:早期,阿里巴巴B2B公司 ...