STL初步:大理石在哪儿?

    xiaoxiao2021-03-25  127

    uva 10474

    现有N个大理石,每个大理石上写了一个非负整数。首先把各数从小到大排序,然后回答Q个问题。每个问题问是否有一个大理石写着某个整数x,如果是,还要回答哪个大理石上写着x。排序后的大理石从左到右编号为1~N。(样例中,为了节约篇幅,所有大理石上的数合并到一行,所有问题也合并到一行。) 样例输入: 4 1  2 3 5 1 5 5 2 1 3 3 3 1   2 3  样例输出: CASE# 1: 5 found at 4 CASE# 2: 2 not found 3 found at 3 提示:sort函数与lower_bound函数结合完成 注:codeblocks测试数据通过 #include<iostream> #include<algorithm> using namespace std; const int maxn=10000; int main() { int n,m,a[maxn],b[maxn],cases=1; while(cin>>n>>m) { for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=m;i++) cin>>b[i]; cout<<"CASE# "<<cases<<":"<<endl; cases++; sort(a+1,a+n+1); for(int i=1;i<=m;i++) { int p=lower_bound(a+1,a+n+1,b[i])-a;//注意·此处 if(a[p]==b[i]) cout<<b[i]<<" found at "<<p<<endl; else cout<<b[i]<<" not found"<<endl; } } return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-15154.html

    最新回复(0)