主脑提示( Master-Mind Hints )

题目

MasterMind is a game for two players. One of them, Designer, selects a secret code. The other, Breaker, tries to break it. A code is no more than a row of colored dots. At the beginning of a game, the players agree upon the length N that a code must have and upon the colors that may occur in a code. In order to break the code, Breaker makes a number of guesses, each guess itself being a code. After each guess Designer gives a hint, stating to what extent the guess matches his secret code. In this problem you will be given a secret code s1 . . . sn and a guess g1 . . . gn, and are to determine the hint. A hint consists 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, and is called weak otherwise. Two matches (i, j) and (p, q) are called independent when i = p if and only if j = q. A set of matches is called independent when all of its members are pairwise independent. Designer chooses an independent set M of matches for which the total number of matches and the number of strong matches are both maximal. The hint then consists of the number of strong followed by the number of weak matches in M. Note that these numbers are uniquely determined by the secret code and the guess. If the hint turns out to be (n, 0), then the guess is identical to the secret code.

输入

The input will consist of data for a number of games. The input for each game begins with an integer specifying N (the length of the code). Following these will be the secret code, represented as N integers, which we will limit to the range 1 to 9. There will then follow an arbitrary number of guesses, 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 to be considered as a guess. Following the data for the first game will appear data for the second game (if any) beginning with a new value for N. The last game in the input will be followed by a single ‘0’ (when a value for N would normally be specified). The maximum value for N will be 1000.

输出

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

输入样例

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

输出样例 

Game 1:

(1,1)

(2,0)

(1,2)

(1,2)

(4,0)

Game 2:

(2,4)

(3,2)

(5,0)

(7,0)

分析题意

原题为英文,可根据题意,可分析为先输入一个数N,接下来是输入 N 个整数的猜测,我们将其限制在 1 到 9 的范围内。然后会有任意数量的猜测,每次输入也表示为 N 个整数,每个整数都在 1 到 9 的范围内。每一段的最后一次输入将是 N 个零; 这些零不应被视为猜测。 在第一段的数据之后将出现第二段的数据(如果有的话),以 N 的新值开头。输入中的最后一场比赛将跟在单个“0”之后(当 N 的值通常为 指定的)。 N 的最大值为 1000。

每段的输出应该列出每个猜测会生成的提示,按顺序一个提示一行。 每个提示应表示为一对用括号括起来并由一个逗号分隔 。前一个数为位置正确的数的个数,即强匹配数,后一个数为强匹配数减去猜测的拥有正确数的个数,即弱匹配数。

代码如下

#include 
#include
#include 
#include 
void  pdd(int h[1000]);
int  love(int a[1000]);
int b[10]={0},yuan[1000],flag=0,n,li[1000];
int main()
{
	int i,j=1,m=0,a[1000];
	while(1)
	{
    	for(i=1;i<10;i++)
			b[i]=0;	
		scanf("%d",&n);
		if(n==0)break;
		printf("Game %d:\n",j);
		j++;
		for(i=0;i=0;i--)
  	if(li[i]==yuan[i]) k++;
  m=love(h);
  printf("    ");
  printf("(%d,%d)\n",k,m-k);
}
int love(int c[1000])
{   int p,d[10]={0},k=0;
	for(p=0;p

附:

编者曾用该题目类似的原理写过一个游戏:

#include
#include
#include
void  pdd(int h[4]);
int  love(int a[4]);
int b[7]={0},yuan[4],flag=0;
int main()
{
    int i,j,a[4],li;
    char m,n;
    while(1){
    srand(time( NULL ));
    for(i=0;i<4;i++){
        yuan[i]=rand()%6+1;
        b[yuan[i]]++;}
    for(i=0;i<10;i++){
    scanf("%d",&li);
    for(j=0;j<4;j++){
    if(j==0)printf("       ");
    a[j]=li%10;
    li/=10;}
    for(j=3;j>=0;j--)
    printf("   %d",a[j]); 
    pdd(a);
    if(flag==1)  {printf("You are right\n");break;}
    } 
  if(flag==0)    printf("Game over\n");
  printf("需要再来一次吗?(Y/N)");
  scanf("%c",&m);
  if(m=='N' || m=='n')
    break;
    }

void pdd(int h[4])
{
  int i,j,m=0,n=0;
  for(i=3;i>=0;i--)
  if(h[i]==yuan[i]) n++;
  m=love(h);
  printf(" || %d %d\n",m,n);
  if(m==4&&n==4) flag=1;
}
int love(int c[4])
{   int p,d[7]={0},k=0;
    for(p=0;p<4;p++){
      d[c[p]]++;
      if(d[c[p]]<=b[c[p]]) k+=1;}
    return k;
}

原理差不多,系统会生成一个由四个随机1---6的数组,即题目,题目不可见。玩家要正确输入对应的数,和题目相同,才可以挑战成功。如果输入错误,系统会给出提示,左边的数为猜测的拥有正确数的个数,即强匹配数-弱匹配数。右边的数为强匹配数。每次共有10次猜测机会。

如随机生成 1 2 3 4,我们猜测为2 3 4 5,输入2345。系统给出提示为3 0。我们猜测为4 2 3 1,输入4231。系统给出提示为4 2。直至我们猜出1 2 3 4,系统会跳出“恭喜成功”。否则超过10次,系统会提示失败。

你可能感兴趣的:(C++,C,算法,c++)