您的位置:首页 > 其它

UVa 10474 Where is the Marble

2015-02-19 13:47 281 查看
题意:给出一列数,先排序,再查找
学习了sort函数,lower_bound函数
sort:可以给任意对象排序(包括自己定义的)(前提是定义好了‘<’运算符)
lower_bound:查找大于或者等于x的第一个位置。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=10005;
int a[maxn];

int main()
{
int n,p,q,x,ncase=1;
while(scanf("%d %d",&n,&q)!=EOF&&n)
{
for(int i=0;i<n;i++) scanf("%d",&a[i]);
sort(a,a+n);
printf("CASE# %d:\n",ncase);
while(q--)
{
scanf("%d",&x);
p=lower_bound(a,a+n,x)-a;

if(a[p]==x) printf("%d found at %d\n",x,p+1);
else printf("%d not found\n",x);
}
ncase++;
}
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: