我自己写的代码

#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. Nginx 和 Apache 开启目录浏览功能

    1.Nginx 在相应项目的 Server 段中的 location 段中,添加 autoindex on.例如: server { listen ; server_name www.dee.prac ...

  2. python学习道路(day7note)(subprocess模块,面向对象)

    1.subprocess模块   因为方法较多我就写在code里面了,后面有注释 #!/usr/bin/env python #_*_coding:utf-8_*_ #linux 上调用python脚 ...

  3. The Path Attribute

    https://tools.ietf.org/html/rfc6265#section-5.1.1 4.1.2.4. The Path Attribute The scope of each cook ...

  4. css background-size 属性 兼容ie8 ie7 方案

    <!-- background-size-polyfill v0.2.0 | (c) 2012-2013 Louis-Rémi Babé | MIT License --> <PUB ...

  5. Mongodb基本操作说明

    Mongodb基本操作说明 1.首先cmd(管理员方式运行)下启动mongo服务(类似初始化工具): Mongod.exe 默认文件夹为 :c:\data\db 如果没有创建该文件夹的话,需要先创建该 ...

  6. markdown命令语法

    markDown作为一款很好用的文档编写工具,具体的用法如下: # Markdown syntax---**粗体***斜体****粗体加斜体*** > 引用>> 二级引用 # 一级标 ...

  7. MVC5中EF6 Code First启动慢及间隙变慢优化的实践经验(转)

    最近项目在使用EF了,mvc使用EF确实方便,因为添加功能的时候可以使用vs自动生成用ef的增.删.查.改的模板,大的提高的工作效率.但是很多人都遇到过用EF开发的程序在第一次访问的时候会比用ADO纯 ...

  8. C# Console控制命令

    参考博客:C# 控制台程序(命令行程序)设置字体颜色,窗口宽高,光标行数 禁用控制台关闭按钮,参考博客:禁用C#控制台应用程序的关闭按钮 参考博客:c# 控制台程序禁用关闭按钮完美解决 #region ...

  9. cocos2dx 3.x(让精灵随着重力感应的方向移动而移动)

    // // GameScene.hpp // HelloWord // // Created by apple on 2017/1/7. // // #ifndef GameScene_hpp #de ...

  10. maven编译报错 -source 1.5 中不支持 lambda 表达式

    在用maven编译项目是由于项目中用了jdk 1.8, 编译是报错  -source 1.5 中不支持 lambda 表达式,Google找到这篇解决方案,记录一下: 编译时报如下错误: [ERROR ...