UVa 10474 Where is the Marble

时间:2021-07-29 05:07:43

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

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn=;
int a[maxn]; int main()
{
int n,p,q,x,ncase=;
while(scanf("%d %d",&n,&q)!=EOF&&n)
{
for(int i=;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+);
else printf("%d not found\n",x);
}
ncase++;
}
return ;
}