大理石在哪(where is the Marble? VUa 10474)

    两个东西,一个是sort函数(默认升序),可以对任意对象进行排序(sort是个模板函数),排序对象可以存在普通数组里,也可以存在vector中。前者用sort(a,a+n),后者用sort(a.begin(),a.end())。一个是lower_bound函数,作用是查找“大于等于X的第一个位置”。(第一次写博客QAQ)

# include <cstdio>
# include <algorithm>
using namespace std;
const int maxn = 10000;
int main(){
	int n, q, x, a[maxn], kase = 0; // n 是总数,q是需要找的数字 
	while(scanf("%d%d",&n, &q) == 2 &&n){
		printf("CASE# %d:\n",++kase); 
		for(int i = 0;i < n; i++)
		scanf("%d",&a[i]);
		sort(a,a+n); // 排序 
		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;
}

// 4 1 
// 2 3 5 1
// 5

 

posted @ 2018-07-21 09:05  UUUUh  阅读(87)  评论(0编辑  收藏  举报