UVA-10474

题目

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;
}

 

转载于:https://www.cnblogs.com/yjz6/p/9799611.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值