E.Mastering Mastermind
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了E.Mastering Mastermind相关的知识,希望对你有一定的参考价值。
题目描述
This sequence of pegs is the code.
The second person (the code breaker) has the job of trying to determine the code maker’s code and she does so by making a series of guesses. Each guess also consists of n colored pegs. After each guess, the code maker gives the code breaker feedback about how close she is. This feedback consists of two number r and s, where
• r = the number of pegs that are identical in both color and position in the code and the guess,andFor example, if the code is BACC (where we use different letters to represent colors) and the guess is CABB, then r = 1 (the A in the second position of both the code and the guess) and s = 2 (a B and C in the remaining three characters). Note that only one of the B’s in the guess will “match” with the single B in the code: once pegs in the code and the guess have been “matched” with each other, they can’t be matched with any other pegs.
• s = the number of remaining pegs that are identical in color but not in the same position.
Your job in this problem is to determine r and s given a code and a guess.
输入
输出
样例输入
4 BACC CABB
样例输出
1 2
CODE:
#include <stdio.h>
#include <string.h>
void dele(char *p)
{
char *i;
for(i=p+1;*i!=‘\0‘;p++,i++)
*p=*i;
*p=‘\0‘;
}
int main()
{
int n,r=0,s=0,j,i;
char *p1;
char a[52],b[52];
while(scanf("%d",&n)!=EOF)
{
getchar();
for(i=0;i<n;i++)
scanf("%c",&a[i]);
a[i]=‘\0‘;
getchar();
for(i=0;i<n;i++)
scanf("%c",&b[i]);
b[i]=‘\0‘;
for(i=0;i<strlen(a);i++)
{
if(a[i]==b[i])
{
r++;
p1=&a[i];
dele(p1);
p1=&b[i];
dele(p1);
i--;
}
}
for(i=0;i<strlen(a);i++)
{
for(j=0;j<strlen(b);j++)
{
if(a[i]==b[j])
{
s++;
p1=&a[i];
dele(p1);
p1=&b[j];
dele(p1);
j--;
i--;
}
}
}
printf("%d %d",r,s);
}
return 0;
}
以上是关于E.Mastering Mastermind的主要内容,如果未能解决你的问题,请参考以下文章
c_cpp MasterMind游戏,以计算机为代码破解者。致力于我的女儿。
mastermind X Chrome Hearts克罗心 联名合作版 全部手工打造