1.sort 和 lower_bound
例如 marble https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1415
1 #include <stdio.h> 2 #include <algorithm> 3 const int maxn = 10000; 4 using namespace std; 5 int main() 6 { 7 //freopen("in","r",stdin); 8 //freopen("out","w",stdout); 9 int n, x,j, Q, i, m[maxn],kase=0; 10 while (scanf("%d%d", &n, &Q) == 2 && n) 11 { 12 for (i = 0; i < n; i++) 13 scanf("%d", &m[i]); 14 sort(m, m + n); 15 printf("CASE# %d:\n",++kase); 16 while (Q--) 17 { 18 scanf("%d", &x); 19 int p = lower_bound(m, m + n, x) - m; 20 if (m[p] == x) 21 printf("%d found at %d\n", x, p+1); 22 else 23 printf("%d not found\n", x); 24 } 25 } 26 return 0; 27 }