大理石在哪里?(Where is the Marble?,UVa 10474 )

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.

解题步骤:

1、把各数从小到大排序(用sort()函数)。

2、然后回答Q个问题,也就是查找特定的“大理石”。

代码如下(有点繁琐):

#include<cstdio>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char a[30]=" found at ",b[30]=" not found",c[30]="CASE# ";
int s[10010],s1[10010],sum[10010];
int main()
{int x,y,sumd=1;while(cin>>x>>y&&x&&y){memset(sum,0,sizeof(sum));//给sum数组空间清零。 for(int i=0;i<x;i++)cin>>s[i];for(int i=0;i<y;i++)cin>>s1[i];//用bubble_sort(s,x),进行排序;sort(s,s+x);for(int i=0;i<y;i++)for(int j=0;j<x;j++){if(s[j]==s1[i]){sum[i]=j+1;//定位 break;}} cout<<c<<sumd<<':'<<endl;sumd++;for(int i=0;i<y;i++)if(sum[i]!=0)cout<<s1[i]<<a<<sum[i]<<endl;elsecout<<s1[i]<<b<<endl;}return 0;}

============================================================================

以下代码更加简洁精炼↓

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn =10000;//const intz作用和C语言里#define相近 int main()
{int n,q,x,a[maxn],kase=0;while(scanf("%d%d",&n,&q)==2&&n){printf("CASE# %d:\n",++kase);for(int i=0;i<n;i++)scanf("%d",&a[i]);sort(a,a+n);//排序 while(q--){scanf("%d",&x);int p=lower_bound(a,a+n,x)-a;if(a[p]==x)printf("%d found at %d\n",x,p+1);elseprintf("%d not found\n",x);}}return 0;
}

大理石在哪里?(Where is the Marble?,UVa 10474 )相关推荐

  1. 大理石在哪儿 (Where is the Marble?,UVa 10474)

    题目描述:算法竞赛入门经典例题5-1 1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 ...

  2. 《算法竞赛入门经典》 例题5-1 大理石在哪(Where is the Marble,UVa 10474)

    原题及翻译 Raju and Meena love to play with Marbles. 拉朱和米娜喜欢玩弹珠. They have got a lot of marbles with numb ...

  3. UVA 10474 Where is the Marble

    题目描述: Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written ...

  4. UVA - 10474

    第五章的第一个大理石的题,附上书上的代码 #include<cstdio> #include<algorithm> using namespace std; const int ...

  5. ICPC训练联盟2021寒假冬令营(7)_2021.01.26_笔记

    文章目录 试题链接 学习笔记 - C++STL.贪心算法 C++STL 迭代器 STL算法 关联式容器 贪心算法 介绍 使用贪心法能否得到最优解,是必须加以证明的. 体验贪心法内涵的实验范例 贪心法的 ...

  6. 很有趣的STL初学资料

    1.泛型程序设计简介与迭代器的介绍 2.常见的STL容器及其例题应用(UVA10474,UVA101,UVA10815,UVA156,UVA540,UVA136 HDU1027,CF501B,HDU1 ...

  7. 紫书《算法竞赛入门经典》

    紫书<算法竞赛入门经典>题目一览 第3章 数组和字符串(例题) UVA 272 TEX Quotes UVA 10082 WERTYU UVA 401 Palindromes UVA 34 ...

  8. 初学者acm的练习题指南

    上机练习题参考题 忘了在哪找的啦~~希望对大家有帮助呦 <!--[if !supportLists]-->1.    <!--[endif]-->Programming Bas ...

  9. 【有利可图网】PS实战教程:PS制作岩石金属镶嵌质感立体文字

    本教程主要使用形状工具. 3D工具.材质以及光线设置来制作,完成的效果比较真实,大家可以跟着教程一步一步的来. 大理石/玫瑰金组合是流行的极简主义趋势之一,几乎无处不在,从家居装饰到文具,炊具等.本教 ...

最新文章

  1. 看论文不用来回翻了,这款PDF阅读神器能自动提取前文信息,科研效率max!
  2. constraint的一些用法总结
  3. C语言-一维数组与指针
  4. redis学习(九)——数据持久化
  5. postman生成python代码_python代码直接生成可执行exe文件
  6. HDU-1540 Tunnel Warfare 线段树最大连续区间 或 STL巧解
  7. JSP三大指令、七大动作、九大对象
  8. 未注册小程序名称-小程序名称大全-周期更新未注册的好名字
  9. 简单测试.NET开源的PDF文档生成器QuestPDF
  10. 65位高校教师接龙晒工资!给打算入高校的研究生们参考!
  11. Ubuntu10.04使用HP LaserJetPro P1606dn
  12. 个人信息安全规范----6、个人信息的委托处理、共享、转让、公开披露
  13. ISP(六) 空间域图像变换(图像反转、对数变换、幂次变换、分段线性变换、直方图均衡与匹配)
  14. 保护计算机组件免受esd,TVS管与ESD保护二极管的区别
  15. access_token VS refresh_token
  16. 用计算机弹怎么会爱上了他,怎么会爱上了他,并决定跟他回家?《纸短情长》...
  17. markdown语法转换成html,使用Showdown来把markdown语法转化成html
  18. win10 tensorflow-gpu1.4 rtx2060 安装详细教程 fater-rcnn
  19. 4298. 【NOIP2015模拟11.2晚】我的天
  20. lemming games 1!! hdlbits

热门文章

  1. android wifi取消自动连接网络,Android 11新增无线网络连接安全策略 可以不自动连接某些特定WiFi...
  2. IntelliJ IDEA必会的几个常用设置
  3. matlab解五元方程,哪位大侠帮忙解这个五元四次方程组
  4. 最新B站优秀C4D教程汇总(超干)
  5. 绿卡日记:2020-11-20
  6. Kindle已连接WiFi网络,但无法连接互联网的解决办法
  7. 怎么用python编简单游戏大全_适合新手练手的三个python简单小游戏
  8. Linux操作手机端
  9. Android 手机上利用adb shell模拟手机相关操作
  10. 中点分割裁剪算法 c语言,裁剪算法——中点分割算法/Liang-Barsky算法