题目
Raju and Meena love to play with Marbles. They have got a lot of marbles with numbers written on them. At the beginning, Raju would place the marbles one after another in ascending order of the numbers written on them. Then Meena would ask Raju to find the first marble with a certain number. She would count 1...2...3. Raju gets one point for correct answer, and Meena gets the point if Raju fails. After some fixed number of trials the game ends and the player with maximum points wins. Today it’s your chance to play as Raju. Being the smart kid, you’d be taking the favor of a computer. But don’t underestimate Meena, she had written a program to keep track how much time you’re taking to give all the answers. So now you have to write a program, which will help you in your role as Raju.
题目链接:https://vjudge.net/problem/UVA-10474
水题就不多说了。
收获:lower_bound(a,a+n,x)
查找大于或者等于x的第一个位置。 返回的是一个指针,所以返回值减去数组可以得到他的下标。
代码
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100000+10;
int a[maxn];
int main(){
int n,q,countn=0;
while(~scanf("%d%d",&n,&q)&&(n||q)){
printf("CASE# %d:\n",++countn);
for(int i=0;i < n;i++){
scanf("%d",&a[i]);
}
sort(a,a + n);
int p;
while(q--&&~scanf("%d",&p)){
int res = lower_bound(a,a+n,p) - a;
if(a[res] != p){
printf("%d not found\n",p);
}else{
printf("%d found at %d\n",p,res+1);
}
}
}
return 0;
}