(先上题目)

(题目描述)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

3

5

1

5

5 2

1

3

3

3

2

0 0

(样例输出)ample Output
CASE# 1: 5 found at 4

CASE# 2: 2 not found

3 found at 3

简要写明一下题目意思:

先输入n和m两个数字,n为大理石的个数,每块大理石上都有一个数字,在下面依次输入这n个数;m代表你接下来要在这些大理石上寻找m个数字,在最后依次输入你要找的这m个数,如果找到了就要输出那个数在大理石上排第几(按从小到大),如果没有找到就输出not found.

我做的时候有参考CSDN上大神的代码啦(忘了是谁)

根据题目呢,这题要排序,于是用sort函数(好像sort函数只能对数组里的数字进行排序的),老样子,在主函数前加上bool cmp便于下面sort排序。

(注意多组输入)先实现n和m,及n个数和m个数的输入。然后因为上面(用蓝色背景标注)的要求,肯定要对大理石上的数字进行排序,于是大理石上的数字用数组a[1000]输入,然后sort(a,a+n,cmp)对大理石排好序了(但1a数组仍然从a[0]开始,但a[0]的值已经不是原来那个了,而是n个数中最小的那个了。接着用循环实现m个数的输入。然后开始找数字,在n个数里找你输入的m个数。用循环for(i=0;i<m;i++) [因为找m个数就行了,所以<m],这时候就用一个新的函数lower_bound,简单写一下这个函数的用处:

lower_bound:【注意待查找数组必须已经排好序!得到的是第一个大于等于表达式3的数组下标!】

迁移到这道题,已经排好序的数组是a[1000],对a数组使用这个函数:pos=lower_bound(a,a+n,k[i])-a,于是就找到了第一个大于等于k[i](需要的数字)在大理石上的数字(这个第一个数字最小也等于所需数字k[i],如果不等于,说明大理石上没有你要找的数字)数组下标,然后用if判断所找的k[i]是否等于大理石数组上的a[pos],如果等于就可以输出is found那个,否则就输出not found那个。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
bool cmp(int a,int b)
{
    return a<b;
}
int s[11000];
int main()
{
    int a,b,c=0;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        if((a==0)&&(b==0))
            break;
        else
        {
            int i,k[11000];
            for(i=0; i<a; i++)
            {
                scanf("%d",&s[i]);/*依次a块大理石上的数字s[i]*/

}

sort(s,s+a,cmp);/*对a块大理石上的数字进行由小到大的排序*/

for(i=0;i<b;i++)
            {
                scanf("%d",&k[i]);/*依次输入需要在大理石上寻找的数字k[i]*/
            }
             printf("CASE# %d:\n",++c);
            for(i=0;i<b;i++)
            {
                int pos=lower_bound(s,s+a,k[i])-s;/*在排好序的大理石上寻找第一个大于等于k[i]的数字,pos为s数组的下标*/
                if(s[pos]==k[i])
                {
                    printf("%d found at %d\n",k[i],pos+1);
                }
                else
                {
                    printf("%d not found\n",k[i]);
                }
            }
        }

}

return 0;
}

Where is the Marble? (寻找大理石上的数字)的更多相关文章

  1. POJ C程序设计进阶 编程题#4:寻找平面上的极大点

    编程题#4:寻找平面上的极大点 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描 ...

  2. COJN 0485 800503寻找平面上的极大点

    800503寻找平面上的极大点 难度级别:C: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 在一个平面上,如果有两个点(x,y),(a,b) ...

  3. KNN识别图像上的数字及python实现

    领导让我每天手工录入BI系统中的数据并判断数据是否存在异常,若有异常点,则检测是系统问题还是业务问题.为了解放双手,我决定写个程序完成每天录入管理驾驶舱数据的任务.首先用按键精灵录了一套脚本把系统中的 ...

  4. 如何使用alt键+数字键盘上的数字键打出特殊符号

    如何使用alt键+数字键盘上的数字键打出特殊符号 有时当我需要画示意图说明一个问题,但是苦于没有合适的符号,因此,这篇博文将简单介绍一些特殊的符号方便自己以及大家使用. 实现原理很简单:所有的字符(包 ...

  5. python 图片上添加数字源代码

    最近因工作需要,需要在图片上添加数字,查询了资料,自己写了一个方法,并进行了测试,由于代码用到了PIL库,需要下载安装,下载地址:http://www.pythonware.com/products/ ...

  6. C#识别图片上的数字

    通过Emgu实现对图片上的数字进行识别. 前期步骤: 1.下载Emgu安装文件,我的版本是2.4.2.1777.3.0版本则实现对中文的支持. 2.安装后需填写环境变量,环境变量Path值后加入Emg ...

  7. [SOJ]寻找第k大数字(numberk)

    Description 经过长时间的筹备工作,在Jourk,Ronny,Plipala,阿长,阿沈等人的努力下,DM实验室建立起自己的系列网站,其中包括三个大板块:DMOJ首页.DMOJ论坛.DMOJ ...

  8. python 识别图片上的数字

    https://blog.csdn.net/qq_31446377/article/details/81708006 ython 3.6 版本 Pytesseract 图像验证码识别 环境: (1) ...

  9. 在Xshell 6开NumLock时按小键盘上的数字键并不能输入数字

    小键盘问题 在Xshell 6上用vi的时候,开NumLock时按小键盘上的数字键并不能输入数字,而是出现一个字母然后换行(实际上是命令模式上对应上下左右的键).解决方法 选项Terminal-> ...

随机推荐

  1. GCD nyoj 1007 (欧拉函数+欧几里得)

    GCD  nyoj 1007 (欧拉函数+欧几里得) GCD 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 The greatest common divisor ...

  2. 解决tcp粘包问题

    目录 什么是粘包(演示粘包现象) 解决粘包 实际应用 什么是粘包 首先只有tcp有粘包现象,udp没有粘包 socket收发消息的原理 发送端可以是一K一K地发送数据,而接收端的应用程序可以两K两K地 ...

  3. mysql 5.7版本后时间datetime 默认为 0000-00-00 00:00:00 问题

    CREATE TABLE `test_user` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `name` char(25) DEFAULT '' ...

  4. c#解决浏览器跨域问题

    1.浏览器为什么不能跨域? 浏览器有一个基本的安全策略--同源策略.为保证用户的信息安全,它对不同源的文档或脚本对当前文档的读写操作做了限制.域名,子域名,端口号或协议不同都属于不同源,当脚本被认为是 ...

  5. eclipse,代码中有错误,项目或者java类中却不显示红叉

    修改eclipse代码提示级别1.单个项目修改项目上右键-->properties-->java compiler-->building-->enable project sp ...

  6. 插入MongoDB文档:mongo控制台查看插入到MongoDB文档中的内容

    const MongoClient = require('mongodb').MongoClient; const assert = require('assert'); const url = 'm ...

  7. ES2018新特性(译文)

    原文链接:css-tricks.com 第9版ECMAScript标准于2018年6月发布,正式名称为ECMAScript 2018(简称ES2018).从ES2016开始,ECMAScript规范的 ...

  8. MySQL 远程连接问题

    使用Workbench 无法远程连接Mysql服务器提示如下错误: 查找原因: 显示只能localhost 访问. 解决方法:修改授权远程访问 create user 'root'@'%' ident ...

  9. 2018-2019-2 网络对抗技术 20165328 Exp1 PC平台逆向破解

    实验目的: 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何用户输入的字符串. 该程序同时包含另一个代码片段,getSh ...

  10. jq实现遮罩等待转圈

    function Show_TopDiv(msg,msg_Width,msg_Height) { var titleheight = "22px"; // 提示窗口标题高度 var ...