UVA10474 Where is the Marble?

链接:https://vjudge.net/problem/UVA-10474

        STL是之C++的标准模板库(Standard Template Library)。

        题意:先排序,后查找。用vector存储排序对象,sort和lower_bound完成排序和查找。

注:vector数组要用迭代器访问,排序时为begin()到end(),每次输入数组必须清空vector。

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=10000;

int main()
{
    int n,q,x,k,kase=0;
    vector <int>a;
    while(scanf("%d%d",&n,&q)==2&&n)
    {
        a.clear();
        printf("CASE# %d:\n",++kase);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&k);
            a.push_back(k);
        }
        sort(a.begin(),a.end());
        while(q--)
        {
            scanf("%d",&x);
            vector<int>::iterator it=lower_bound(a.begin(),a.end(),x);
            if(*it==x)
                printf("%d found at %d\n",x,it-a.begin()+1);
            else
                printf("%d not found\n",x);
        }

    }
    return 0;
}
AC Code

 

posted @ 2018-08-20 16:49  子诚-  阅读(141)  评论(0编辑  收藏  举报