15.大理石在哪儿(Uv10474)

问题:

输入n个整数,代表大理石编号;再输入q个数(编号),问是否有这个编号的大理石,位置在哪里?

输入:
4 1
2 3 5 1
5
5 2
1 3 3 3 1
2 3

输出:
Sample Output
CASE# 1:
5 found at 4
CASE# 2:
2 not found
3 found at 3

解决:

#include 
#include 
#define MAXN 11000
using namespace std;

int marble[MAXN];
int main(){
	int n, q, caseno = 0, val; 
	// n 个大理石猜 q次
	while (scanf("%d%d", &n, &q) != EOF) {
		if (n == 0 && q == 0)
			break;
		for (int i = 0; i<n; i++)
			scanf("%d", &marble[i]);
		sort(marble, marble + n);
		printf("CASE# %d:\n", ++caseno);
		while (q--) {
			scanf("%d", &val);
			int no = lower_bound(marble, marble + n, val) - marble;
			if (marble[no] == val)
				printf("%d found at %d\n", val, no + 1);
			else
				printf("%d not found\n", val);
		}
	}

	return 0;
}

你可能感兴趣的:(算法基础)