UVa 340 - Master-Mind Hints

在写这个程序之前,让我们先学习一下英语.╮(╯▽╰)╭

In this problem you will be given a secret code tex2html_wrap_inline35 and a guess tex2html_wrap_inline37 , and are to determine the hint.

在这个问题中,你将会被提供一个密码s1....sn 和一个猜测g1...gn ,这将会决定暗示.

A hint consists of a pair of numbers determined as follows.

一个暗示由一对数字组成,如下文所说.

match is a pair (i,j), tex2html_wrap_inline41 and tex2html_wrap_inline43 , such that tex2html_wrap_inline45 .

一个匹配就是一对(i,j),比如tex2html_wrap_inline45

Match (i,j) is called strong when i = j

当i = j时,匹配(i,j)被称为强匹配.

and is called weak otherwise.

否则被称为弱匹配.

The hint then consists of the number of strong followed by the number of weak matches in M.

暗示由(强匹配的数目,之后为弱匹配的数目)组成.

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

输入从一个指定的N(密码的长度)开始.之后一行就是密码,由N个整数表示.

There will then follow an arbitrary number of guesses, each also represented as N integers, each in the range 1 to 9. 

接着就是任意的猜测数据,每个也用N个整数表示,每个数字范围为1-9

The maximum value for N will be 1000.

N最大为1000

The output for each game should list the hints that would be generated for each guess

每次游戏的输出应该列出由每次猜测产生的暗示.


我觉得这题出个英语阅读理解肯定能坑死很多人.

题意是这样的.

输入:

第一行,密码的长度.

第二行,正确的密码,

接下来每行输入的都是猜测的密码.也就是说每次输入都要和正确的密码对比,然后输出一个结果

输出:

(a,b) ,a是强匹配的数目,b是真·弱匹配的数目.

当输入的第i行第i列等于密码的第i行第i列时,这是一个强匹配

也就是说,密码和输入在同一列上的数字相等时,算一个强匹配.

比如

密码:1 2 3 3

猜测:1 2 5 7

强匹配数目就是2

OK..弄清了什么是强匹配之后,再来看下弱匹配..

伪·弱匹配就是

如果密码和猜测之间有相同的数字,就算一个伪·弱匹配.但因为是匹配,所以一个数字只能用一次.


比如

密码:            3 3 2 5

猜测(Input)1 3 5 2

他的伪·弱匹配就是3.

为什么加个伪呢.....因为真·弱匹配 = 伪·弱匹配 - 强匹配

也就是说,强匹配的优先级比弱匹配高.

因此上一个例子的强匹配就是1. 真·弱匹配 = 3 - 1 = 2

所以输出为(1,2)

这样应该清楚题意了吧......

对于每组数据,先输入一个密码.然后以后的输入都和这个密码对照,得出强匹配和真·弱匹配的数目.然后输出.

说到底也很简单..

我才不会说我看了一个小时的题目呢╮(╯▽╰)╭

最后的最后,记得数组开大点.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
    //freopen("input.txt", "r", stdin);
    char word[1100];
    char buf[1100];
    int i, j, n, count = 1;
    int strong, weak;
    int num1[20] = {0};
    int num2[20] = {0};
    while (scanf("%d", &n) == 1)
    {
		if (n == 0)
			break;
        memset(num1, 0, sizeof(num1));
        printf("Game %d:\n", count++);
        getchar();
        gets(word);
        for (i = 0; i < strlen(word); i++)
            if (isdigit(word[i]))
                num1[word[i] - '0']++;
        while (gets(buf))
        {
            strong = weak = 0;
            if (buf[0] == '0')
                break;
            for (i = 0; i < strlen(buf); i++)
                if (isdigit(buf[i]))
                    num2[buf[i] - '0']++;
            for (i = 0; i < strlen(word); i++)
				if (isdigit(word[i]) && isdigit(buf[i]))
						if (word[i] == buf[i])
							strong++;
            for (i = 1; i < 10; i++)
            {
                if (num1[i] && num2[i])
                {
                    if (num1[i] > num2[i])
                        weak += num2[i];
                    else
                        weak += num1[i];
                }
            }
            weak -= strong;
            printf("    (%d,%d)\n", strong, weak);
            memset(buf, 0, sizeof(buf));
            memset(num2, 0, sizeof(num2));
        }
    }
    return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值