大理石在哪儿(UVa10474)

  

  题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=835&page=show_problem&problem=1415

C++ 11代码如下:

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 int num[10000];
 5 int main() {
 6     int q, n, t,k=0;
 7     while ((cin >> n >> q) && n != 0) {
 8         cout << "CASE# " << ++k << ':' << endl;
 9         for (int i = 0; i < n; i++) cin >> num[i];
10         sort(num, num + n);  //默认升序
11         while (q--) {
12             cin >> t;
13             int p = lower_bound(num, num + n, t) - num;  //lower_bound输出大于或者等于t的第一个位置,下面还需判断此位置处元素是否等于t
14             if (num[p] == t) cout << t << " found at " << p+1 << endl; //大理石从1开始排序,所以输出p+1
15             else cout << t << " not found" << endl;
16         }
17     }
18     return 0;
19 }

猜你喜欢

转载自www.cnblogs.com/pgzhang/p/9267807.html