UVA 10474 - Where is the Marble?--vector
https://vjudge.net/problem/UVA-10474
https://blog.csdn.net/xiyaozhe/article/details/81081344
简单用法
sort(start,end) 默认是升序
实现降序:
#include <functional>
int a[10]={5,6,7,8,9,0,1,2,3,4};
vector <int> v(a, a+10);
sort(v.begin(), v.end(),less<int>());//升
sort(v.begin(), v.end(),greater<int>());//降
find(起点, 终点后一位, 要找的数)
distance (地址, 地址) 是返回容器中两个地址之间的距离
vector基本操作
ve.push_back(a);
作用是在数组后面增加一个元素。括号里填的是ve里装的东西
ve.clear();清空ve里的所有元素。
ve.empty();判断ve是否为空,如果是返回true,否则false
ve.size();返回ve的长度。注意这里返回的类型是unsigned int,如果ve是空的ve.size() - 1就会爆掉。
ve.pop_back() 删除数组里的最后一个元素。
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<queue>
#include<vector>
#define mem(a) memset(a,0,sizeof(a))
using namespace std;
const long long N=1e9+;
#define LL long long int main()
{
int n,m,number,ca=;
vector<int> ma;
while(cin>>n>>m&&n!=&&m!=)
{
for(int i=;i<n;i++)
{
cin>>number;
ma.push_back(number);
}
sort(ma.begin(),ma.end());
cout<<"CASE# "<<++ca<<":"<<endl;
for(int i=;i<m;i++)
{
cin>>number;
vector<int>::iterator it =find(ma.begin(),ma.end(),number);
if(it==ma.end())
cout << number << " not found"<<endl;
else
cout<<number<<" found at "<<distance(ma.begin(),it)+<<endl;
}
ma.clear();
}
return ;
}
UVA 10474 - Where is the Marble?--vector的更多相关文章
- UVA.10474 Where is the Marble ( 排序 二分查找 )
UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了 ...
- UVa 10474 Where is the Marble
题意:给出一列数,先排序,再查找学习了sort函数,lower_bound函数sort:可以给任意对象排序(包括自己定义的)(前提是定义好了‘<’运算符)lower_bound:查找大于或者等于 ...
- uva 10474 Where is the Marble? 计数排序
题目给出一系列数字,然后问哪个数字是从小到大排在第几的,重复出现算第一个. 数据范围为10000,不大,完全可以暴力,sort不会超时. 但是由于以前做比赛时也遇到这种题目,没注意看数据范围,然后暴力 ...
- uva 10474 Where is the Marble?(简单题)
我非常奇怪为什么要把它归类到回溯上,明明就是简单排序,查找就OK了.wa了两次,我还非常不解的怀疑了为什么会 wa,原来是我居然把要找的数字也排序了,当时仅仅是想着能快一点查找.所以就给他排序了,没考 ...
- 【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 ...
- Where is the Marble UVA - 10474
Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on th ...
- 大理石在哪?(Where is the Marble?,UVa 10474)
参考:ACM紫书 第五章 P108 [排序与检索] 下面的代码中有些 提示性输出,想Ac 需删除提示性输出语句,读者自行修改. #include <cstdio> #include < ...
- 大理石在哪儿 (Where is the Marble?,UVa 10474)
题目描述:算法竞赛入门经典例题5-1 #include <iostream> #include <algorithm> using namespace std; ; int m ...
- 大理石在哪儿(Where is the Marble?,Uva 10474)
现有N个大理石,每个大理石上写了一个非负整数.首先把各数从小到大排序,然后回 答Q个问题.每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上 写着x.排序后的大理石从左到右编号为1 ...
随机推荐
- java extends和implements区别
一.作用说明 extends 是继承某个类, 继承之后可以使用父类的方法, 也可以重写父类的方法; implements 是实现多个接口, 接口的方法一般为空的, 必须重写才能使用 二.补充 JAVA ...
- react native环境搭建与生命周期
1.搭建开发环境 英文文档:http://facebook.github.io/react-native/docs/getting-started.html 中文文档:https://reactnat ...
- (转)Spring事务管理详解
背景:之前一直在学习数据库中的相关事务,而忽略了spring中的事务配置,在阿里面试时候基本是惨败,这里做一个总结. 可能是最漂亮的Spring事务管理详解 https://github.com/Sn ...
- gprof性能测试工具
1.编译时加-pg选项,例如:gcc -pg test.c-o test_gprof.其中test后的_gprof一定要加上.会生成gmon.out. 2.运行程序.gprof test_gprof ...
- ElasticSearch6.5.0 【Java客户端之REST Client】
说明 High Level Client 是基于 Low Level Client 的.官方文档如下: * https://www.elastic.co/guide/en/elasticsearch/ ...
- go interface接口
一:接口概要 接口是一种重要的类型,他是一组确定的方法集合. 一个接口变量可以存储任何实现了接口方法的具体值.一个重要的例子就是io.Reader和io.Writer type Reader inte ...
- LeetCode 92. ReverseLinkedII
#include <iostream> using namespace std; struct ListNode { int val; ListNode *next; ListNode(i ...
- 第十一节:Bundles压缩合并js和css及原理分析
一. 简介 1.背景:浏览器默认一次性请求的网络数是有上限的,如果你得js和css文件太多,就会导致浏览器需要多次加载,影响页面的加载速度, MVC中提供Bundles的方式压缩合并js和css,是M ...
- 第七节: EF的三种事务的应用场景和各自注意的问题(SaveChanges、DBContextTransaction、TransactionScope)
一. 什么是事务 我们通俗的理解事务就是一系列操作要么全部成功.要么全部失败(不可能存在部分成功,部分失败的情况). 举一个事务在我们日常生活中的经典例子:两张银行卡(甲.乙),甲向乙转钱,整个过程需 ...
- MySQL学习7 - 外键的变种 三种关系
一 介绍 二 如何找两张表之间的关系 三 表的三种关系 1.书和出版社 2.作者和书籍的关系 3.用户和博客 本节的重点 如何找出两张表之间的关系 表的三种关系 一 介绍 因为有foreign key ...