首页 > 其他 > 详细

where is the marble?

时间:2021-01-28 17:51:39      阅读:26      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 

 

#include<iostream>
#include<algorithm>
using namespace std;

int marbles[10005];

int main(void)
{
    int N, Q;
    int kcase = 0;
    while (scanf("%d %d", &N, &Q) == 2 && N)
    {
        printf("CASE# %d:\n", ++kcase);
        for (int i = 0; i < N; i++)
            scanf("%d", &marbles[i]);
        int p;
        sort(marbles, marbles + N);//先给大理石排序
        while (Q--)
        {
            int q;
            scanf("%d", &q);
            p = lower_bound(marbles, marbles + N, q)-marbles;//lower_bound是查找大于或等于x的第一个位置,因为返回值为该位置地址,所以减去数组第一个地址得到该位置下标
            if (marbles[p] == q) printf("%d found at %d\n", q, p+1);
            else printf("%d not found\n", q);
        }
    }
    return 0;
}

 

where is the marble?

原文:https://www.cnblogs.com/loliconsk/p/14340890.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!