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
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? (寻找大理石上的数字)的更多相关文章
- POJ C程序设计进阶 编程题#4:寻找平面上的极大点
编程题#4:寻找平面上的极大点 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描 ...
- COJN 0485 800503寻找平面上的极大点
800503寻找平面上的极大点 难度级别:C: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 在一个平面上,如果有两个点(x,y),(a,b) ...
- KNN识别图像上的数字及python实现
领导让我每天手工录入BI系统中的数据并判断数据是否存在异常,若有异常点,则检测是系统问题还是业务问题.为了解放双手,我决定写个程序完成每天录入管理驾驶舱数据的任务.首先用按键精灵录了一套脚本把系统中的 ...
- 如何使用alt键+数字键盘上的数字键打出特殊符号
如何使用alt键+数字键盘上的数字键打出特殊符号 有时当我需要画示意图说明一个问题,但是苦于没有合适的符号,因此,这篇博文将简单介绍一些特殊的符号方便自己以及大家使用. 实现原理很简单:所有的字符(包 ...
- python 图片上添加数字源代码
最近因工作需要,需要在图片上添加数字,查询了资料,自己写了一个方法,并进行了测试,由于代码用到了PIL库,需要下载安装,下载地址:http://www.pythonware.com/products/ ...
- C#识别图片上的数字
通过Emgu实现对图片上的数字进行识别. 前期步骤: 1.下载Emgu安装文件,我的版本是2.4.2.1777.3.0版本则实现对中文的支持. 2.安装后需填写环境变量,环境变量Path值后加入Emg ...
- [SOJ]寻找第k大数字(numberk)
Description 经过长时间的筹备工作,在Jourk,Ronny,Plipala,阿长,阿沈等人的努力下,DM实验室建立起自己的系列网站,其中包括三个大板块:DMOJ首页.DMOJ论坛.DMOJ ...
- python 识别图片上的数字
https://blog.csdn.net/qq_31446377/article/details/81708006 ython 3.6 版本 Pytesseract 图像验证码识别 环境: (1) ...
- 在Xshell 6开NumLock时按小键盘上的数字键并不能输入数字
小键盘问题 在Xshell 6上用vi的时候,开NumLock时按小键盘上的数字键并不能输入数字,而是出现一个字母然后换行(实际上是命令模式上对应上下左右的键).解决方法 选项Terminal-> ...
随机推荐
- 012_TCP keepalive 和 http keep-alive
TCP keepalive概念在使用TCP长连接(复用已建立TCP连接)的场景下,需要对TCP连接进行保活,避免被网关干掉连接.在应用层,可以通过定时发送心跳包的方式实现.而Linux已提供的TCP ...
- js数据结构与算法——集合
<script> function Set(){ var items = {};//使用对象表示集合,因为js对象不允许一个键指向两个不同的值,保证集合里面的匀速唯一性 this.add ...
- 利用python进行数据加载和存储
1.文本文件 (1)pd.read_csv加载分隔符为逗号的数据:pd.read_table从文件.URL.文件型对象中加载带分隔符的数据.默认为制表符.(加载为DataFrame结构) 参数name ...
- Hanlp学习笔记
一.首先要引入mawen依赖包: <dependency> <groupId>com.hankcs</groupId> <artifactId>hanl ...
- 2018-2019-2 网络对抗技术 20165206 Exp3 免杀原理与实践
- 2018-2019-2 网络对抗技术 20165206 Exp3 免杀原理与实践 - 实验任务 1 正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion,自己 ...
- .net core ef 通过dbfirst方式连接sql server数据库
1. 创建基于.net core 的项目(过程略) 2. 使用nuget添加引用 Microsoft.EntityFrameworkCore Microsoft.EntityFrameworkCore ...
- php curl Problem with the SSL CA cert (path? access rights?)
公司有台老服务器,搭的php的环境,有个负载均横的服务 调用 curl_init 的时候报了 Problem with the SSL CA cert (path? access rights?) 网 ...
- thinkPHP实现APP微信支付
控制器 class Pay extends Controller { const WX_PAY_URL = "https://api.mch.weixin.qq.com/pay/unifie ...
- [奇思异想]使用Zookeeper管理数据库连接串
背景 有一套特定规格的应用(程序+数据库),当有业务需求时,就需要多部署应用,并且所有的应用都使用一个共同的后台来管理.应用新增后,如何通知后台更新连接串成了一个关键的问题.于是就产生了使用ZooKe ...
- Excel—图表函数
OFFSET(引用函数)它可以找一个区域,把一个区域的值都拿回来 函数语法:OFFSET(从哪个单元格开始,这个单元格下移多少行开始,然后现在的位置再往右多少列开始,要取的值是现在的位置开始下移多少行 ...