UVA 10474 Where is the Marble

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

主要是练习lower_bound 的应用:返回一个迭代器,指向键值为k的第一个元素。而且也很方便的找出排序后需查找的元素的位置。

cin和c里面的输入输出用的话好像不太好,但是不想改了。

 1 #include <bits/stdc++.h>
 2 
 3 #define N 100010
 4 #define maxn 200010
 5 
 6 using namespace std;
 7 
 8 typedef long long int ll;
 9 
10 int main()
11 {
12     vector<int> a;
13     vector<int>::iterator ite;
14     int n, i, q, num;
15     i=0;
16     while(cin>>n>>q , n||q){
17         a.clear();  //清空容器
18         while(n--){
19             cin>>num;
20             a.push_back(num);
21         }
22         sort(a.begin(), a.end());
23         int x;
24         printf("CASE# %d:\n", ++i);
25         while(q--){
26             cin>>x;
27             ite=lower_bound(a.begin(), a.end(), x);
28             if(*ite==x){
29                 printf("%d found at %d\n", x, ite-a.begin()+1);
30             }
31             else printf("%d not found\n", x);
32         }
33     }
34     return 0;
35 }

猜你喜欢

转载自www.cnblogs.com/Arrokoth/p/12213164.html
今日推荐