UVa-10474-大理石在哪
lower_bound()的作用是查找“大于或等于x的第一个位置”,但是返回的是地址,所以减去数组的首地址就是偏移量了,也就是整型数字。
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 10000;
int main()
{
int n, q, x, a[maxn], kase = 0;
while (scanf("%d%d",&n,&q)==2&&n) {
for (int i = 0; i < n;i++)
scanf("%d", &a[i]);
sort(a, a + n);
printf("CASE# %d:\n", ++kase);
while (q--) {
scanf("%d", &x);
int p = lower_bound(a, a + n, x) - a;
if (a[p]==x)
printf("%d found at %d\n", x, p+1);
else
printf("%d not found\n", x);
}
}
return 0;
}