大理石在哪里UVa 10474
我自己写的代码
#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的更多相关文章
- UVA.10474 Where is the Marble ( 排序 二分查找 )
UVA.10474 Where is the Marble ( 排序 二分查找 ) 题意分析 大水题一道.排序好找到第一个目标数字的位置,返回其下标即可.暴力可过,强行写了一发BS,发现错误百出.应了 ...
- 大理石在哪儿(Where is the Marble?,Uva 10474)
现有N个大理石,每个大理石上写了一个非负整数.首先把各数从小到大排序,然后回 答Q个问题.每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上 写着x.排序后的大理石从左到右编号为1 ...
- UVA 10474 大理石在哪 lower_bound
题意:找输入的数在排完序之后的位置. 主要是lower_bound 函数的使用.它的作用是查找大于或者等于x的第一个位置. #include<cstdio> #include<alg ...
- 大理石在哪?(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 ...
- 【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 ...
- UVA 10474
题意:给你一组数,再给几个数问是否在一组数中. 题很简单:STL入门. 没用到STL. #include<iostream> #include<cstdio> #include ...
- UVa 10474 Where is the Marble
题意:给出一列数,先排序,再查找学习了sort函数,lower_bound函数sort:可以给任意对象排序(包括自己定义的)(前提是定义好了‘<’运算符)lower_bound:查找大于或者等于 ...
- uva 10474 Where is the Marble? 计数排序
题目给出一系列数字,然后问哪个数字是从小到大排在第几的,重复出现算第一个. 数据范围为10000,不大,完全可以暴力,sort不会超时. 但是由于以前做比赛时也遇到这种题目,没注意看数据范围,然后暴力 ...
随机推荐
- Rails--抛出异常
begin ... rescue Exception => e ... end
- Mybatis缓存处理机制
一.MyBatis缓存介绍 正如大多数持久层框架一样,MyBatis 同样提供了一级缓存和二级缓存的支持 一级缓存: 基于PerpetualCache 的 HashMap本地缓存,其存储作用域为 Se ...
- Mac下,使用sshpass让iterm2支持多ssh登录信息保存
windows里有个Xshell非常的方便好使,因为它能保存你所有的ssh登录帐号信息.MAC下并没有xshell,有些也提供这样的功能,但效果都不好.iterm2是很好的终端,但却不能很好的支持多p ...
- 简单的jquery tab
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Resharper 8.2 注册码
用户名:ronle注册码:ZoJzmeVBoAv9Sskw76emgksMMFiLn4NM
- :active 为什么在ios上失效
:active是针对鼠标,而手机上是没有鼠标,而是touchstart,所以早成了ios上不兼容 解决方法是: window.onload = function(){ document.body.ad ...
- (copy) Top Ten Reasons not to use the C shell
http://www.grymoire.com/Unix/CshTop10.txt ========================================================== ...
- lua UT测试工具
luaunit Luaunit is a unit-testing framework for Lua, in the spirit of many others unit-testing frame ...
- SLP测试记录
个人感觉来说这个游戏对我没有什么吸引力...完全不知道用户需求在哪...是我最不喜欢的一个游戏 不过听制作团队之前的介绍,这应该不算是一个游戏,而是一个游戏练习器?所以从游戏的角度来评判的话感觉有很多 ...
- mongodb群集
项目目标:故障自动切换和自动修复成员节点,各个数据库之间数据完全一致.项目描述:副本集没有固定主节点,是整个集群选举得出的一个主节点,当其不工作时变 更其他节点.最小的副本集也应该具备一个pri ...