我自己写的代码

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int N,a[100],b[100],Q,flag;
    int k=1;
    while(cin>>N>>Q)
    {
        for(int i=0;i<N;i++)
        {
            cin>>a[i];
        }
        for(int j=0;j<Q;j++)
        {
            cin>>b[j];
        }
        sort(a,a+N);

for(int j=0;j<Q;j++)
        {
            flag=0;
            cout<<"CASE# "<<k++<<endl;
           for(int i=0;i<N;i++)
           {
               if(a[i]==b[j])
               {
                   cout<<b[j]<<" found at "<<i+1<<endl;
                   flag=1;
                   break;
               }
           }
           if(flag==0)
           {
               cout<<b[j]<<" not found"<<endl;
           }
        }
    }
    return 0;
}

书上写的代码

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
    int N,a[100],Q,x;
    int k=1;
    while(cin>>N>>Q)
    {
        for(int i=0;i<N;i++)
        {
            cin>>a[i];
        }
        sort(a,a+N);
        cout<<"CASE# "<<k++<<endl;
        while(Q--)
        {
            cin>>x;
            int p=lower_bound(a,a+N,x)-a;//大于等于x的第一个数的下标
            if(a[p]==x)
                cout<<x<<" found at "<<p+1<<endl;
            else
                cout<<x<<" not found"<<endl;
        }

}
    return 0;
}

哎,自己还是菜的不行啊

大理石在哪里UVa 10474的更多相关文章

  1. UVA.10474 Where is the Marble ( 排序 二分查找 )

    UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了 ...

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

    现有N个大理石,每个大理石上写了一个非负整数.首先把各数从小到大排序,然后回 答Q个问题.每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上 写着x.排序后的大理石从左到右编号为1 ...

  3. UVA 10474 大理石在哪 lower_bound

    题意:找输入的数在排完序之后的位置. 主要是lower_bound 函数的使用.它的作用是查找大于或者等于x的第一个位置. #include<cstdio> #include<alg ...

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

    参考:ACM紫书 第五章 P108 [排序与检索] 下面的代码中有些 提示性输出,想Ac 需删除提示性输出语句,读者自行修改. #include <cstdio> #include < ...

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

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

  6. 【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 ...

  7. UVA 10474

    题意:给你一组数,再给几个数问是否在一组数中. 题很简单:STL入门. 没用到STL. #include<iostream> #include<cstdio> #include ...

  8. UVa 10474 Where is the Marble

    题意:给出一列数,先排序,再查找学习了sort函数,lower_bound函数sort:可以给任意对象排序(包括自己定义的)(前提是定义好了‘<’运算符)lower_bound:查找大于或者等于 ...

  9. uva 10474 Where is the Marble? 计数排序

    题目给出一系列数字,然后问哪个数字是从小到大排在第几的,重复出现算第一个. 数据范围为10000,不大,完全可以暴力,sort不会超时. 但是由于以前做比赛时也遇到这种题目,没注意看数据范围,然后暴力 ...

随机推荐

  1. java容器collection的一些简单特点

    1.List ArrayList 可随机访问元素,但中间插入和一处元素较慢 LinkedList 在中间进行的插入和删除操作代价较小,随机访问比ArrayList较慢 特性集比ArrayList大 2 ...

  2. Linux 升级修改libc gcc 文件名称,导致执行命令失效问题解决

    升级linux文件时,若不小心把文件名给重命名了,结果导致执行所有命令都不识别. 比如我们不小心执行了 mv /lib64/libc.so.6 /lib64/libc.so.6.bak 结果导致所有系 ...

  3. C++学习笔记 封装 继承 多态 重写 重载 重定义

    C++ 三大特性 封装,继承,多态 封装 定义:封装就是将抽象得到的数据和行为相结合,形成一个有机的整体,也就是将数据与操作数据的源代码进行有机的结合,形成类,其中数据和函数都是类的成员,目的在于将对 ...

  4. win7下安装mysql5.7[zip包]

    原本以为很简单的安装,结果卡在一个环节,此文记录安装步奏. 1.下载mysql-5.7.16-winx64.zip 安装包 地址:http://cdn.mysql.com//Downloads/MyS ...

  5. Resolving SQL Server Disk IO bottlenecks

    网上看到这篇文章挺不错的,直接翻译过来.在尝试诊断SQL Server性能时,不要仅仅依赖某个单一的诊断数据,比如CPU的使用率.SQL Server磁盘性能,就得出结论却忽略的问题的根源.实际上,使 ...

  6. Linux上设置memcached自启动

    #!/bin/sh # # memcached: MemCached Daemon # # chkconfig: - 90 25 # description: MemCached Daemon # # ...

  7. iOS,视图相关

    1.移除视图的所以子视图 2.自定义视图(UIView) 3.处理悬浮窗口(类似微信视频),等比缩放 4.自定义前面视图(可以手写字) 5.图片拉伸的几种方式,计算文本占用空间大小 6.UILable ...

  8. Design and Analysis of Algorithms_Fundamentals of the Analysis of Algorithm Efficiency

    I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...

  9. The prefix "util" for element "util:list" is not bound.

    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4. ...

  10. HTML之Hello World

    之前学过一段时间的HTML相关知识,但是已经有将近一年多时间没有用过了,曾经学过的知识基本都忘记了,所以要从头开始学习. 例子:Hello World <!DOCTYPE html> &l ...