uvaoj 10474

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:
",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
",ans);
22             if(a[ans]==temp)printf("%d found at %d
",temp,ans+1);//重点 
23             else printf("%d not found
",temp);
24         }
25     }
26     return 0;
27 }