您的位置:首页 > 其它

UVA 340 Master-Mind Hints

2015-07-26 19:45 211 查看

题目

猜数字游戏的提示

分析

每组测试样例,第一行给出数字个数,第二行给出正确的数字,接下来每行均为对数字的猜测一组猜测,直到猜测均为一组
0
时,该组测试样例结束。

程序需要对每次猜测进行统计,统计出有多少个猜测正确(即相同位置的数字相等)以及有多少个数字仅是位置不对。

思路

对于每一组猜测数字,统计各数字的出现次数,首先匹配正确数字,并减少次数;再判断是否有猜测的数字在正确数字中存在但不匹配(即还有没有被匹配的正确数字)。

代码

#include <stdio.h>
#include <string.h>

int main(void)
{
int ocr[10], pas[10], code[1005], guess[1005];
int t, n, i, p, q;
for (t = 1; scanf("%d", &n) && n != 0; t++) {
printf("Game %d:\n", t);
memset(ocr, 0, sizeof(ocr));
memset(code, 0, sizeof(code));
for (i = 0; i < n; i++) {
scanf("%d", &code[i]);
ocr[code[i]]++;
}
while (1) {
p = q = 0;
for (i = 0; i < n; i++)
scanf("%d", &guess[i]);
if (guess[0] == 0) break;
for (i = 0; i < 10; i++) pas[i] = ocr[i];
for (i = 0; i < n; i++) {
if (guess[i] == code[i]) {
p++;
pas[guess[i]]--;
}
}
for (i = 0; i < n; i++) {
if (guess[i] != code[i]) {
/* printf("guess: %d, pas: %d\n", guess[i], pas[guess[i]]); */
if (pas[guess[i]]) {
q++;
pas[guess[i]]--;
}
}
}
printf("    (%d,%d)\n", p, q);
}

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: