Where is the Marble? 

Raju and Meena love to play with Marbles. They have got a lotof marbles with numbers written on them. At the beginning, Rajuwould place the marbles one after another in ascending order ofthe numbers written on them. Then Meena would ask Raju tofind the first marble with a certain number. She would count1...2...3. Raju gets one point for correct answer, and Meena getsthe point if Raju fails. After some fixed number of trials thegame ends and the player with maximum points wins. Today it'syour chance to play as Raju. Being the smart kid, you'd be takingthe favor of a computer. But don't underestimate Meena, she hadwritten a program to keep track how much time you're taking togive all the answers. So now you have to write a program, whichwill 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 consistsbegins with 2 integers:N the number of marbles and Q the number of queries Mina wouldmake. The next N lines would contain the numbers written on the N marbles. These marblenumbers will not come in any particular order. FollowingQ lines will have Q queries. Beassured, 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 andQ = 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 uponwhether or not the query number is written upon any of the marbles. The two different formatsare described below:

  • `x found at y', if the first marble with numberx was found at position y.Positions are numbered1, 2,..., N.
  • `x not found', if the marble with numberx is not present.

Look at the output for sample input for details.

Sample Input

4 1
2
3
5
1
5
5 2
1
3
3
3
1
2
3
0 0

Sample Output

CASE# 1:
5 found at 4
CASE# 2:
2 not found
3 found at 3

题意:

每一组数据的第一行包含两个数, 第一个数N是石头数, 第二个数Q是要找的石头编号

接下来N个石头, 数值是它们的编号

然后排序, 看看我们要找的石头是排在第几个.

做法:

我第一反应是桶排序, 不过自己还有一种方法, 所以先用自己想的方法写了一遍, 结果WA了

我的想法是: 不用排序, 一个for循环过去, 统计那些 编号值 比 我们要找的石头的编号 要小的石头个数, 并且, 标志一下数组里是否有我们要找的石头.

这个想法是简单直接的, 但是我也不知道错在哪里, 最后乖乖桶排序AC了

下面两份代码, 第一份是AC代码, 第二份是WA代码, 个人还是在深究第二份为何错, 我喜欢自己的想法~

AC:

#include<stdio.h>
#include<string.h> int main() {
int N, Q;
int cas = 0;
while(scanf("%d %d", &N, &Q) != EOF) {
if(N == 0 && Q == 0)
break; int num[10005], sum[10005];
int tmp, aim; memset(sum, 0, sizeof(sum));
memset(num, 0, sizeof(num)); for(int i = 1; i <= N; i++) {
scanf("%d", &tmp);
num[tmp]++;
} printf("CASE# %d:\n", ++cas); for(int i = 1; i <= 10000; i++)
sum[i] = num[i] + sum[i-1]; for(int i = 1; i <= Q; i++) {
scanf("%d", &aim);
if(num[aim])
printf("%d found at %d\n", aim, sum[aim-1] + 1);
else
printf("%d not found\n", aim);
}
}
return 0;
}

WA:

#include<stdio.h>

int N, Q;
int cas = 0; int main() {
while(scanf("%d %d", &N, &Q) != EOF) {
if(N == 0 && Q == 0)
break; int aim, pos, mark;
int num[10001]; for(int i = 0; i < N; i++)
scanf("%d", &num[i]); printf("CASE# %d:\n", ++cas); for(int i = 0; i < Q; i++) {
scanf("%d", &aim);
pos = 1;
mark = 0;
for(int j = 0; j < N; j++) {
if(num[j] < aim)
pos++;
if(num[j] == aim)
mark = 1;
}
if(mark == 1)
printf("%d found at %d\n", aim, pos);
else
printf("%d not found\n", aim);
}
}
return 0;
}

UVA 10474 (13.08.04)的更多相关文章

  1. UVA 156 (13.08.04)

     Ananagrams  Most crossword puzzle fans are used to anagrams--groupsof words with the same letters i ...

  2. UVA 10194 (13.08.05)

    :W Problem A: Football (aka Soccer)  The Problem Football the most popular sport in the world (ameri ...

  3. UVA 253 (13.08.06)

     Cube painting  We have a machine for painting cubes. It is supplied withthree different colors: blu ...

  4. UVA 10790 (13.08.06)

     How Many Points of Intersection?  We have two rows. There are a dots on the toprow andb dots on the ...

  5. UVA 573 (13.08.06)

     The Snail  A snail is at the bottom of a 6-foot well and wants to climb to the top.The snail can cl ...

  6. UVA 10499 (13.08.06)

    Problem H The Land of Justice Input: standard input Output: standard output Time Limit: 4 seconds In ...

  7. UVA 10025 (13.08.06)

     The ? 1 ? 2 ? ... ? n = k problem  Theproblem Given the following formula, one can set operators '+ ...

  8. UVA 465 (13.08.02)

     Overflow  Write a program that reads an expression consisting of twonon-negative integer and an ope ...

  9. UVA 10494 (13.08.02)

    点此连接到UVA10494 思路: 采取一种, 边取余边取整的方法, 让这题变的简单许多~ AC代码: #include<stdio.h> #include<string.h> ...

随机推荐

  1. git更新到仓库

    记录每次更新到仓库 现在我们手上已经有了一个真实项目的 Git 仓库,并从这个仓库中取出了所有文件的工作拷贝.接下来,对这些文件作些修改,在完成了一个阶段的目标之后,提交本次更新到仓库. 请记住,工作 ...

  2. Wannafly挑战赛9 D - 造一造

    链接:https://www.nowcoder.com/acm/contest/71/D来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...

  3. 关于CSRF的那点事儿

    0x01 CSRF简介     CSRF,也称XSRF,即跨站请求伪造攻击,与XSS相似,但与XSS相比更难防范,是一种广泛存在于网站中的安全漏洞,经常与XSS一起配合攻击. 0x02 CSRF原理 ...

  4. OpenGL笔记<第一章> 构建 GLSL class

    恭喜,我们终于很扎实地完成了第一章——glsl 入门 不幸的是,it's not the basic of GLSL shader ,我们下一节开篇,basic of GLSL shader 在下一章 ...

  5. iOS WKWebview 网页开发适配指南

    iOS WKWebview 网页开发适配指南 微信iOS客户端将于2017年3月1日前逐步升级为WKWebview内核,需要网页开发者提前做好网站的兼容检查和适配.如有问题,可参考文末联系方式,向我们 ...

  6. python 并集union, 交集intersection, 差集difference, 对称差集symmetric_difference

    python的集合set和其他语言类似,是一个无序不重复元素集, 可用于消除重复元素. 支持union(联合), intersection(交), difference(差)和sysmmetric d ...

  7. [ 原创 ]学习笔记-Android 学习笔记 Contacts (一)ContentResolver query 参数详解 [转载]

    此博文转载自:http://blog.csdn.net/wssiqi/article/details/8132603 1.获取联系人姓名 一个简单的例子,这个函数获取设备上所有的联系人ID和联系人NA ...

  8. 汇编代码中db,dw,dd的区别

    db定义字节类型变量,一个字节数据占1个字节单元,读完一个,偏移量加1 dw定义字类型变量,一个字数据占2个字节单元,读完一个,偏移量加2 dd定义双字类型变量,一个双字数据占4个字节单元,读完一个, ...

  9. bzoj 2406 二分+有源有汇上下界网络流可行流判定

    弱爆了,典型的行列建模方式,居然想不到,题做少了,总结少了...... 二分答案mid s----------------------->i行-----------------------> ...

  10. bash编程之 ~制作Mini Linux系统~

    说明1:在一个Linux宿主机系统上,通过以上步骤,可以制作一个微小的Linux系统(可以放置在U盘中等),然后在其它的主机(虚拟机或者物理机)上运行,以实现最小化定制系统的目的. 说明2:上图中黑色 ...