Uva - 10474 - Where is the Marble?


主要用到排序和查找,sort和lower_bound两个STL就搞定了

注意把“CASE# 1:”这个放到输入N个大理石之前和放到输入查找的大理石之前都可以AC

AC代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <map>
#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;
}



posted @ 2015-06-11 17:10  Say舞步  阅读(156)  评论(0编辑  收藏  举报