Uva-10474

https://vjudge.net/problem/UVA-10474

思路:

对输入的数排序,在已排好序的数中,利用lower_bound()函数查找。

 

基本用法:lower_bound(a,a+n,x);其中a为数组或容器名,n为数组容量,x为要查找值。(通过二分查找实现)

x的位置是:int position=lower_bound(a,a+n,x)-a;

注意使用前数组必须有序才能达到描述效果。

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

 

posted @ 2019-04-09 00:13  zuiaimiusi  阅读(128)  评论(0编辑  收藏  举报