题目描述
利用STL中的sort( )和lower_bound( )直接实现,函数lower_bound( ) 返回的是被查序列中第一个大于等于查找值的指针
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=10000;
int main(){
int n,q,x,a[maxn],kase=0;
while(scanf("%d%d",&n,&q)==2&&n){
printf("CASE# %d:\n",++kase);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
while(q--){
scanf("%d",&x);
int p=lower_bound(a,a+n,x)-a;
if(a[p]==x) printf("%d found at %d\n",x,p+1);
else printf("%d not found\n",x);
}
}
}