uvaoj 10474 - Where is the Marble?(sort+lower_bound)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1415

STL中sort和lower_bound(返回大于或者等于x的第一个位置)应用

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int a[10005];
 4 int main()
 5 {
 6     int n,q,cases=0;
 7     while(~scanf("%d %d",&n,&q),n+q)
 8     {
 9         cases++;
10         printf("CASE# %d:\n",cases);
11         for(int i=0;i<n;i++)
12         {
13             scanf("%d",&a[i]);
14         }
15         sort(a,a+n);
16         while(q--)
17         {
18             int temp;
19             scanf("%d",&temp);
20             int ans=lower_bound(a,a+n,temp)-a;
21             //printf("%d\n",ans);
22             if(a[ans]==temp)printf("%d found at %d\n",temp,ans+1);//重点 
23             else printf("%d not found\n",temp);
24         }
25     }
26     return 0;
27 }

 

posted @ 2018-11-13 16:31  柠檬加糖  阅读(161)  评论(0编辑  收藏  举报