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-> ...
随机推荐
- Java中int和String类型之间转换
int –> String int i=123; String s=""; 第一种方法:s=i+""; //会产生两个String对象 第二种方法:s=S ...
- WPF 10天修炼 第八天 - 形状、画刷和变换
图形 在WPF中使用绘图最简单的就是使用Shape类.Shape类继承自FrameworkElement,是一个专门用来绘图的类.Shape类中年派生的类有直线.矩形.多边形和圆形等. System. ...
- Uni-app页面生命周期
学习任何框架必须将其生命周期了然于胸. uni-app支持如下页面生命周期函数: onLoad 监听页面加载,其参数为上个页面传递的数据,参数类型为object(用于页面传参),示例中可参考 onSh ...
- C语言中return 0和return 1和return -1
转载声明:本文系转载文章 原文作者:十一月zz 原文地址:https://blog.csdn.net/baidu_35679960/article/details/77542787 1.返回值int ...
- scikit-learn中机器学习模型比较(逻辑回归与KNN)
本文源自于Kevin Markham 的模型评估:https://github.com/justmarkham/scikit-learn-videos/blob/master/05_model_eva ...
- unity中加载场景不销毁以及切换场景重复实例化
问题描述 游戏开发中会有多个场景,有时会有这样的需求,我们需要保证场景跳转但是需要保持某个游戏对象不被销毁,比如:音乐 实现思路 unity中提供了DontDestroyOnLoad(),这个API ...
- SpringBoot 动态切换多数据源
1. 配置文件application-dev.properties 2. 动态切换数据源核心 A. 数据源注册器 B. 动态数据源适配器 C. 自定义注解 D. 动态数据源切面 E. 数据源路 ...
- OpenCV中的KNN
一.K近邻 有两个类,红色.蓝色.我将红色点标记为0,蓝色点标记为1.还要创建25个训练数据,把它们分别标记为0或者1.Numpy中随机数产生器可以帮助我们完成这个任务 import cv2 impo ...
- Fisher–Yates shuffle 洗牌算法
Fisher-Yates shuffle 是一种生成有限序列的随机排列的算法--简单地说,该算法可以对序列进行混排.本人能力有限,且懒.不会扒论文去研究该算法在数学上的证明,只能抄袭网上的博客总结一遍 ...
- Angular 自定义拖拽指令
指令 组件是一种带模版的指令.指令是超级. 结构型指令(改变布局)和属性型指令(改变外观和行为). Renderer2和ElementRef Angular不提倡直接操作DOM 对于DOM的操作应该通 ...