where is the marble?

 

 

 

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

 

posted @ 2021-01-28 17:07  loliconsk  阅读(60)  评论(0)    收藏  举报