码迷,mamicode.com
首页 > 其他好文 > 详细

问题 E: Mastering Mastermind

时间:2016-06-11 00:32:17      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

题目描述

Mastermind is a two-person code breaking game which works as follows. The ?rst person (the code maker) creates a sequence of n colored pegs (with duplicate colors allowed) and hides it from view.
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,and
• s = the number of remaining pegs that are identical in color but not in the same position.
For 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.
Your job in this problem is to determine r and s given a code and a guess.

输入

The input is a single line containing a positive integer n ≤ 50 (the length of the code) followed by two strings of length n — the ?rst of these is the code and the second is the guess. Both code and guess are made up of upper-case alphabetic characters.

输出

Output the values of r and s for the given input.
程序设计:
 #include<stdio.h>
#include<string.h>
int main() {
    int n,i,j;
    int q=0,w=0,l;
    char x[50];
    char y[50];
scanf("%d",&n);
getchar();
    for(i=0;i<n;i++) {
scanf("%c",&x[i]);
    }
    getchar();
    for(i=0;i<n;i++) {
scanf("%c",&y[i]);
    }
    for(i=0;i<n;i++) {
        if(x[i]==y[i]) {
            q++;
x[i]=‘0‘;
y[i]=‘0‘;
        }
}
    for(i=0;i<n;i++) {
for(j=0;j<n;j++) {
if(x[i]==y[j]&&x[i]!=‘0‘&&y[j]!=‘0‘) {
w++;
x[i]=‘0‘;
y[j]=‘0‘;
}
}
}
printf("%d %d\n",q,w);
}

问题 E: Mastering Mastermind

标签:

原文地址:http://www.cnblogs.com/tong69/p/5574542.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!