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 }
原文地址:https://www.cnblogs.com/pgzhang/p/9267807.html
时间: 2024-11-01 11:36:03