(先上题目)

(题目描述)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. windows2012服务器中安装php7+mysql5.7+apache2.4环境

    1.下载安装apache.2.4 https://home.apache.org/~steffenal/VC14/binaries/httpd-2.4.38-win64-VC14.zip 解压到d盘的 ...

  2. c++入门篇八

    构造函数的调用规则: 系统会提供三个函数,一个是默认的构造函数(无参,函数体为空),一个是拷贝构造函数(无参,函数体为空),一个是析构函数,对类中非静态成员属性简单值拷贝\如果用户定义了拷贝构造函数, ...

  3. rsync问题

    问题一: rsync: chgrp "/data/www/vhosts/go/.rest.qXYFW5" (in apache) failed: Operation not per ...

  4. django实现SSO

    前言 公司的各种运维平台越来越多,用户再每个平台都注册账号,密码,密码太多记不住不说,然后有的平台过一段时间还得修改密码,烦!还不如弄个统一登录平台!! 需求分析 造这辆大车,首先就得造两个轮子 首先 ...

  5. Python 包管理(PYPA)

    Python包的管理可以通过Python 自带的管理 工具,例如:package-autoremove,package-list-packages, package-install 等,使用起来也非常 ...

  6. python requests下载图片

    # 文件下载方法 from urllib.request import urlretrieve import requests # 第一 urlimage = 'https://www.python. ...

  7. Python3——坦克大战

    # coding=utf-8 # Version:python3.6.1 __date__ = '2018/9/20 18:51' __author__ = 'Lgsp_Harold' import ...

  8. springmvc 控制器 读取properties文件

    配置文件app.properties如下: yt.api.url=http://localhost:9000 springmvc.xml 增加配置: <context:property-plac ...

  9. Extjs6.2 项目学习系列(一)

     1. Extjs开发准备  (1).下载ExtJS  SDK GPL版 (本测试用版本 ext-6.2.0) : https://www.sencha.com/legal/gpl/ (2).下载Se ...

  10. 20175305张天钰 《java程序设计》第四周课下测试总结

    第四周课下测试总结 错题 某方法在父类的访问权限是public,则子类重写时级别可以是protected. A .true B .false 正确答案:B 解析:书P122:子类不允许降低方法的访问权 ...