zoukankan      html  css  js  c++  java
  • 九度OJ 1150:Counterfeit Dollar(假美元) (分析、检验)

    时间限制:1 秒

    内存限制:32 兆

    特殊判题:

    提交:485

    解决:215

    题目描述:

        Sally Jones has a dozen Voyageur silver dollars. However, only eleven of the coins are true silver dollars; one coin is counterfeit even though its color and size make it indistinguishable from the real silver dollars. The counterfeit coin has a different weight from the other coins but Sally does not know if it is heavier or lighter than the real coins. 
        Happily, Sally has a friend who loans her a very accurate balance scale. The friend will permit Sally three weighings to find the counterfeit coin. For instance, if Sally weighs two coins against each other and the scales balance then she knows these two coins are true. Now if Sally weighs one of the true coins against a third coin and the scales do not balance then Sally knows the third coin is counterfeit and she can tell whether it is light or heavy depending on whether the balance on which it is placed goes up or down, respectively. 
        By choosing her weighings carefully, Sally is able to ensure that she will find the counterfeit coin with exactly three weighings.

    输入:

        For each case, the first line of input is an integer n (n > 0) specifying the number of cases to follow. Each case consists of three lines of input, one for each weighing. Sally has identified each of the coins with the letters A--L. Information on a weighing will be given by two strings of letters and then one of the words ``up'', ``down'', or ``even''. The first string of letters will represent the coins on the left balance; the second string, the coins on the right balance. (Sally will always place the same number of coins on the right balance as on the left balance.) The word in the third position will tell whether the right side of the balance goes up, down, or remains even.

    输出:

        For each case, the output will identify the counterfeit coin by its letter and tell whether it is heavy or light. The solution will always be uniquely determined.

    样例输入:
    1
    ABCD EFGH even 
    ABCI EFJK up 
    ABIJ EFGH even 
    样例输出:
    K is the counterfeit coin and it is light.
    来源:
    2010年北京大学计算机研究生机试真题

    思路:

    简单的说,有12枚硬币,1枚是假的,重量与其他的不一样,但不知道是重还是轻。有一个天平可以称,只要三次就能找出来。

    现在给出三次称的结果,求那一枚是假的,重还是轻。

    有两种方法,一种是分析,需要较强的逻辑;还有一种方法我更推荐,直接24种情况代入检验,满足结果即是答案。

    我在九度上的AC代码在POJ上未通过,原因是不一定天平两边都是四枚硬币。

    代码1是九度上的C代码,代码2是在POJ上的C++代码。


    代码1:

    #include <stdio.h>
    #include <string.h>
     
    int c2i(char c)
    {
        return c-'A';
    }
     
    char i2c(int i)
    {
        return i+'A';
    }
     
    int trans(char s[])
    {
        if (strcmp(s, "even") == 0)
            return 0;
        else if (strcmp(s, "up") == 0)
            return 1;
        else
            return -1;
    }
     
    int main(void)
    {
        int i, j, k, r;
        int n, res[3];
        char s1[3][5], s2[3][5], s3[3][5];
     
        while (scanf("%d", &n) != EOF)
        {
            while (n--)
            {
                for (i=0; i<3; i++)
                {
                    scanf("%s%s%s", s1[i], s2[i], s3[i]);
                    res[i] = trans(s3[i]);
                }
                int flag = 0;;
                for (j=-1; j<=1; j+=2)
                {
                    for (k=0; k<12; k++)
                    {
                        for (i=0; i<3; i++)
                        {
                            int value = 0;
                            for (r=0; r<4; r++)
                            {
                                if (c2i(s1[i][r]) == k)
                                    value = j;
                                if (c2i(s2[i][r]) == k)
                                    value = -j;
                            }
                            if (value != res[i])
                                break;
                        }
                        if (i == 3)
                        {
                            flag = 1;
                            break;
                        }
                    }
                    if (flag == 1)
                        break;
                }
                if (j == -1)
                    printf("%c is the counterfeit coin and it is light.
    ", i2c(k));
                else
                    printf("%c is the counterfeit coin and it is heavy.
    ", i2c(k));
            }
        }
     
        return 0;
    }
    /**************************************************************
        Problem: 1150
        User: liangrx06
        Language: C
        Result: Accepted
        Time:0 ms
        Memory:912 kb
    ****************************************************************/
    

    代码2:

    #include <stdio.h>
    #include <string.h>
     
    int c2i(char c)
    {
        return c-'A';
    }
     
    char i2c(int i)
    {
        return i+'A';
    }
     
    int trans(char s[])
    {
        if (strcmp(s, "even") == 0)
            return 0;
        else if (strcmp(s, "up") == 0)
            return 1;
        else
            return -1;
    }
     
    int main(void)
    {
        int i, j, k, r;
        int n, res[3];
        char s1[3][5], s2[3][5], s3[3][5];
     
        while (scanf("%d", &n) != EOF)
        {
            while (n--)
            {
                for (i=0; i<3; i++)
                {
                    scanf("%s%s%s", s1[i], s2[i], s3[i]);
                    res[i] = trans(s3[i]);
                }
                int flag = 0;;
                for (j=-1; j<=1; j+=2)
                {
                    for (k=0; k<12; k++)
                    {
                        for (i=0; i<3; i++)
                        {
                            int value = 0;
                            for (r=0; r<4; r++)
                            {
                                if (c2i(s1[i][r]) == k)
                                    value = j;
                                if (c2i(s2[i][r]) == k)
                                    value = -j;
                            }
                            if (value != res[i])
                                break;
                        }
                        if (i == 3)
                        {
                            flag = 1;
                            break;
                        }
                    }
                    if (flag == 1)
                        break;
                }
                if (j == -1)
                    printf("%c is the counterfeit coin and it is light.
    ", i2c(k));
                else
                    printf("%c is the counterfeit coin and it is heavy.
    ", i2c(k));
            }
        }
     
        return 0;
    }
    /**************************************************************
        Problem: 1150
        User: liangrx06
        Language: C
        Result: Accepted
        Time:0 ms
        Memory:912 kb
    ****************************************************************/
    



    编程算法爱好者。
  • 相关阅读:
    centos6.5的开机自动部署出现unsupported hardware detected
    Nginx的安装
    sshpass的使用方法
    dhcp 的安装和配置文件
    SMBus总线概述
    SMBus与I2C的差别
    vim搭建笔记
    pcie dma的玩法
    Virtex6 PCIe 超简版基础概念学习(二)
    揭开Altera公司支持OpenCL的设计工具的神秘面纱
  • 原文地址:https://www.cnblogs.com/liangrx06/p/5083881.html
Copyright © 2011-2022 走看看