UVA10474 Where is the Marble?

问题链接:UVA10474 Where is the Marble?

题意简述:输入n个整数,代表大理石编号;再输入q个数(编号),问是否有这个编号的大理石,位置在哪里?

这个问题用C++语言编写程序,主要是为了练习使用STL的功能。

程序中,使用了算法库(algorithm)中的两个函数;使用sort()函数用于对数据排序,该函数的参数比C语言的同类函数简单,程序更加易于书写;使用函数lower_bound()查找元素,简单方便。

AC的C++语言程序如下:

/* UVA10474 Where is the Marble? */

#include <iostream>
#include <algorithm>

using namespace std;

#define MAXN 11000

int marble[MAXN];

int main()
{
    int n, q, caseno=0, val;

    while(scanf("%d%d", &n, &q) != EOF) {
        if(n == 0 && q == 0)
            break;

        for(int i=0; i<n; i++)
            scanf("%d", &marble[i]);

        sort(marble, marble + n);

        printf("CASE# %d:\n", ++caseno);
        while(q--) {
            scanf("%d", &val);
            int no = lower_bound(marble, marble + n, val) - marble;
            if(marble[no] == val)
                printf("%d found at %d\n", val, no + 1);
            else
                printf("%d not found\n", val);
        }
    }

    return 0;
}
时间: 2024-12-23 21:54:12

UVA10474 Where is the Marble?的相关文章

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?【排序】

参考: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"

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

排序与检索【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

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? 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

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

zjuoj 3605 Find the Marble

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3605 Find the Marble Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice and Bob are playing a game. This game is played with several identical pots and one marble. When the game starts

Winter-2-STL-C Where is the Marble? 解题报告及测试数据

Time Limit:3000MS     Memory Limit:0KB Description Download as PDF 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 or