3-4 猜数字游戏的提示(Master-Mind Hints, UVa 340)

3-4 猜数字游戏的提示(Master-Mind Hints, UVa 340)

实现一个经典"猜数字"游戏。给定答案序列和用户猜的序列,统计有多少数字位置正确(A),有多少数字在两个序列都出现过但位置不对(B)。

输入包含多组数据。每组输入第一行为序列长度n,第二行是答案序列,接下来是若干猜测序列。猜测序列全0时该组数据结束。n=0时输入结束。

样例输入:

4

1 3 5 5

1 1 2 3

4 3 3 5

6 5 5 1

6 1 3 5

1 3 5 5

0 0 0 0

10

1 2 2 2 4 56 6 6 9

1 2 3 4 5 67 8 9 1

1 1 2 2 3 34 4 5 5

1 2 1 3 1 51 6 1 9

1 2 2 5 5 56 6 6 7

0 0 0 0 0 00 0 0 0

0

样例输出:

Game 1:

     (1,1)

     (2,0)

     (1,2)

     (1,2)

     (4,0)

Game 2:

    (2,4)

    (3,2)

    (5,0)

    (7,0)

【分析】

直接统计可得A,为了求B,对于每个数字(1~9),统计二者出现的次数c1和c2,则min(c1,c2)就是该数字对B的贡献。最后要减去A的部分。代码如下:

#include<stdio.h>
#define maxn 1010
int main() 
{
	int n, a[maxn], b[maxn];
	int kase = 0;
	while(scanf("%d", &n) == 1 && n) { //n=0时输入结束
		printf("Game %d:\n", ++kase);
		for(int i = 0; i < n; i++) scanf("%d", &a[i]);
		for(;;) {
			int A = 0, B = 0;
			for(int i = 0; i < n; i++) {
				scanf("%d", &b[i]);
				if(a[i] == b[i]) A++;
			}
			if(b[0] == 0) break; //正常的猜测序列不会有0,所以只判断第一个数是否为0即可
			for(int d = 1; d <= 9; d++) {
				int c1 = 0, c2 = 0; //统计数字d在答案序列和猜测序列中各出现多少次
				for(int i = 0; i < n; i++) {
					if(a[i] == d) c1++;
					if(b[i] == d) c2++;
				}
				if(c1 < c2) B += c1; else B += c2;
			}
			printf("    (%d,%d)\n", A, B-A);//注意格式需空4个格子 
		}
	}
	return 0;
}

原文如下:

340 - Master-Mind Hints

MasterMind is a game for two players. One of them, Designer, selectsa secret code. The other, Breaker, tries to break it. A code is no more than arow of colored dots. At the beginning of a game, the players agree upon thelength N that a code must have and upon the colors that may occur in a code. Inorder to break the code, Breaker makes a number of guesses, each guess itselfbeing a code. After each guess Designer gives a hint, stating to what extentthe guess matches his secret code. In this problem you will be given a secretcode s1 . . . sn and a guess g1 . . . gn, and are to determine the hint. A hintconsists of a pair of numbers determined as follows. A match is a pair (i, j),1 ≤ i ≤ n and 1 ≤ j ≤ n, such that si = gj . Match (i, j) is called strong when i = j, andis called weak otherwise. Two matches (i, j) and (p, q) are called independentwhen i = p if and only if j = q. A set of matches is called independent whenall of its members are pairwise independent.

Designer chooses an independent set M of matches for which the totalnumber of matches and the number of strong matches are both maximal. The hintthen consists of the number of strong followed by the number of weak matches inM. Note that these numbers are uniquely determined by the secret code and theguess. If the hint turns out to be (n, 0), then the guess is identical to thesecret code.

Input

The input will consist of data for a number of games. The input foreach game begins with an integer specifying N (the length of the code).Following these will be the secret code, represented as N integers, which wewill limit to the range 1 to 9. There will then follow an arbitrary number ofguesses, each also represented as N integers, each in the range 1 to 9.Following the last guess in each game will be N zeroes; these zeroes are not tobe considered as a guess.

Following the data for the first game will appear data for thesecond game (if any) beginning with a new value for N. The last game in theinput will be followed by a single ‘0’ (when a value for N would normally bespecified). The maximum value for N will be 1000.

Output

The output for each game should list the hints that would begenerated for each guess, in order, one hint per line. Each hint should berepresented as a pair of integers enclosed in parentheses and separated by acomma. The entire list of hints for each game should be prefixed by a headingindicating the game number; games are numbered sequentially starting with 1.Look at the samples below for the exact format.

Sample Input

4

1 3 5 5

1 1 2 3

4 3 3 5

6 5 5 1

6 1 3 5

1 3 5 5

0 0 0 0

10

1 2 2 2 4 5 6 6 6 9

1 2 3 4 5 6 7 8 9 1

1 1 2 2 3 3 4 4 5 5

1 2 1 3 1 5 1 6 1 9

1 2 2 5 5 5 6 6 6 7

0 0 0 0 0 0 0 0 0 0

0

Sample Output

Game 1:

    (1,1)

    (2,0)

    (1,2)

    (1,2)

    (4,0)

Game 2:

    (2,4)

    (3,2)

    (5,0)

    (7,0)


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追梦2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值