uva 10474 - Where is the Marble?

太简单了没啥好说的,排下序然后搜索下,看不出要怎么用回溯法。

 

 

#include<stdio.h>
#include<stdlib.h>

#define MAX 10005

int marbles[MAX];

int cmp( const void*a, const void*b) {

	return *(int*) a - *(int*) b;
}

int search(int n, int q) {
	int i;
	for (i = 0; i < n; i++)
		if (marbles[i] == q)
			return i + 1;
	return -1;
}

int main() {
	int n, q;
	int query, idx;
	int cases = 1;
	while (scanf("%d%d", &n, &q)) {
		if (n == 0)
			break;
		int i;
		for (i = 0; i < n; i++)
			scanf("%d", &marbles[i]);
		qsort(marbles, n, sizeof(int), cmp);
		printf("CASE# %d:\n", cases++);

		for (i = 0; i < q; i++) {
			scanf("%d", &query);
			idx = search(n, query);
			if (idx == -1) {
				printf("%d not found\n", query);
			} else {
				printf("%d found at %d\n", query, idx);
			}
		}

	}

	return 0;
}

 

你可能感兴趣的:(where)