zoukankan      html  css  js  c++  java
  • E.Mastering Mastermind

    题目描述

    Mastermind is a two-person code breaking game which works as follows. The first 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 first 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.

    样例输入

    4 BACC CABB
    

    样例输出

    1 2
    
    CODE:

    #include <stdio.h>

    #include <string.h>

    void dele(char *p)

    {

          char *i;

          for(i=p+1;*i!='';p++,i++)

               *p=*i;

          *p='';

    }

    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]='';

              getchar();        

               for(i=0;i<n;i++)

                     scanf("%c",&b[i]);

               b[i]='';

               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;

    }

  • 相关阅读:
    C++数据类型与C#对应关系 c#调用WINDWOS API时,非常有用(转)
    Web应用系统中关闭Excel进程
    jquery下一个空格带来的血案
    导出Excel时发生COM组件失败的解决方案
    水晶报表的交叉表中增加超级链接
    JavaScript和ExtJS的继承 Ext.extend Ext.applyIf (转)
    SQL SERVER 2000数据库置疑处理
    PHP中对淘宝URL中ID提取
    树莓派+蓝牙适配器连接蓝牙设备
    树莓派摄像头模块转成H264编码通过RTMP实现Html输出
  • 原文地址:https://www.cnblogs.com/shu233/p/5575573.html
Copyright © 2011-2022 走看看