UVA10474 Where is the Marble?【排序】

参考:https://blog.csdn.net/q547550831/article/details/51326321

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 #define N 10000
 6 int main()
 7 {
 8     int n,q,count=0;
 9
10     while (scanf("%d %d",&n,&q)!=EOF)
11     {
12         if (n==0&&q==0)
13         {
14             break;
15          }
16         int m[N];
17         int i;
18         for (i=0;i<n;i++)
19         {
20             scanf("%d",&m[i]);
21         }
22         sort(m,m+n);
23         printf("CASE# %d:\n",++count);//此行要放在while前面!!!
24         while (q--)
25         {
26             int temp;
27             scanf("%d",&temp);
28             int t=lower_bound(m,m+n,temp)-m;
29             if (m[t]==temp)
30             {
31                 printf("%d found at %d\n",temp,t+1);
32             }
33             else
34             {
35                 printf("%d not found\n",temp);
36             }
37         }
38     }
39
40     return 0;
41 } 

原文地址:https://www.cnblogs.com/hemeiwolong/p/9375078.html

时间: 2024-10-17 21:59:37

UVA10474 Where is the Marble?【排序】的相关文章

UVA10474 Where is the Marble?

问题链接:UVA10474 Where is the Marble?. 题意简述:输入n个整数,代表大理石编号:再输入q个数(编号),问是否有这个编号的大理石,位置在哪里? 这个问题用C++语言编写程序,主要是为了练习使用STL的功能. 程序中,使用了算法库(algorithm)中的两个函数:使用sort()函数用于对数据排序,该函数的参数比C语言的同类函数简单,程序更加易于书写:使用函数lower_bound()查找元素,简单方便. AC的C++语言程序如下: /* UVA10474 Wher

UVa10474 Where is the Marble ? 有序数组二分找值 lower_bound / upper_bound

题意: 给出n个数,先把各数从小到大排序,然后q次询问xi在数组中的位置,不存在则输出相应信息. 输入样例: 4 1 2 3 5 1 5 5 2 1 3 3 3 1 2 3 0 0 输出样例: CASE# 1: 5 found at 4 CASE# 2: 2 not found 3 found at 3 //======================================= 数组从小到大有序. lower_bound :查找"大于或者等于x"的第一个位置. upper_bo

排序与检索【UVa10474】Where is the Marble?

Where is the Marble?  Description 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 the

【UVA - 10474 】Where is the Marble?(排序)

Where is the Marble? Descriptions: 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 th

UVA10474 大理石在哪儿 Where is the Marble?

UVA10474 大理石在哪儿 Where is the Marble? 题意翻译 现有N个大理石,每个大理石上写了一个非负整数.首先把各数从小到大排序,然后回 答Q个问题.每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上 写着x.排序后的大理石从左到右编号为1-N. 输入输出样例: //输入 4 1 4个大理石 1个问题 2 2 3 5 1 大理石上写的数字 3 5 1 5 这个5是问题 是否有一个大理石写着5 5 2 1 3 3 3 1 2 3 0 0 //输出 CAS

uva 10474 Where is the Marble?(排序)

uva 10474 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.

uva10474大理石在哪儿where is the marble?

背景:做了这么久的题,唯一一道一次ac的,可见这道题是如何的简单. 思路:思路很清楚的模拟题,先排序再查找. 学习:sort函数和lower_bound函数,sort函数排序就不多说了,lower_bound函数作用是查找一个数组中大于等于x的第一个位置. #include <iostream> #include <stdio.h> #include <algorithm> using namespace std; int figue[10000]; void prin

UVA 10474 - Where is the Marble?

Where is the Marble?  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 writ

小白书练习题5.5.3 排序检索类、

UVA 340 Master-Mind Hints 题意:猜数字游戏,给n个数的序列给你.接下来一行是答案序列.剩下的都是猜测序列.对于每一个猜测序列,统计有多少个数字相同并且位置相同.有多少数字相同位置不同.每一个数字只能用一次. 思路:直接统计可以求出数字相同并且位置相同的哪一些数.在此过程中我加了一个标记数组.标记那些用过的数的位置为1,没用过为0:然后枚举猜测中哪些没用过的数字.去答案序列中找.当数字相等并且答案行中那个数也没用过时.计数加1: 1 #include<cstdio> 2