UVa 10474 Where is the Marble

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

 1 #include<iostream>  
 2 #include<cstdio>  
 3 #include<cstring>  
 4 #include<algorithm>  
 5 using namespace std;
 6 
 7 const int maxn=10005;
 8 int a[maxn];
 9 
10 int main()
11 {
12     int n,p,q,x,ncase=1;
13     while(scanf("%d %d",&n,&q)!=EOF&&n)
14     {  
15         for(int i=0;i<n;i++) scanf("%d",&a[i]);
16         sort(a,a+n);
17         printf("CASE# %d:\n",ncase);
18         while(q--)
19         {
20         scanf("%d",&x);
21         p=lower_bound(a,a+n,x)-a;
22         
23         if(a[p]==x) printf("%d found at %d\n",x,p+1);
24         else printf("%d not found\n",x);                        
25         }
26         ncase++;
27     }
28 return 0;    
29 }
View Code

 

posted @ 2015-02-19 13:47  sequenceaa  阅读(145)  评论(0编辑  收藏  举报