uva 340 - Master-Mind Hints

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=98&page=show_problem&problem=276

 

C++ AC。先遍历对比一遍得出strong的数量,然后对每个数字一次遍历weak的情况,一旦match之后都置为-1,以避免重复计算。

#include<cstdio>

const int MAX = 1005;

int code[MAX];
int main() {
	setbuf(stdout,NULL);
	int n;
	int counter = 1;
	while (scanf("%d", &n)) {
		if (n == 0)
		break;
		for (int i = 0; i < n; i++)
		scanf("%d", &code[i]);

		printf("Game %d:\n", counter++);

		int guess[MAX];

		while (true) {
			for (int i = 0; i < n; i++)
			scanf("%d", &guess[i]);

			if (guess[0] == 0)
			break;

			int tmpCode[MAX];
			for (int i = 0; i < n; i++)
			tmpCode[i] = code[i];

			//determint the hint
			int strong = 0, weak = 0;

			for (int i = 0; i < n; i++) {
				if (tmpCode[i] == guess[i]) {
					strong++;
					tmpCode[i] = guess[i] = -1;
				}

			}

			for (int i = 0; i < n; i++)
			if (guess[i] != -1) {
				for (int j = 0; j < n; j++) {
					if (guess[i] == tmpCode[j]) {
						weak++;
						guess[i] = tmpCode[j] = -1;
						break;
					}

				}

			}

			printf("    (%d,%d)\n",strong,weak);

		}

	}
	return 0;
}

 

你可能感兴趣的:(uva 340 - Master-Mind Hints)