zoukankan      html  css  js  c++  java
  • FZU1003 Counterfeit Dollar

    Problem Description 题目链接 

    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.

     Input

    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.

     Output

    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.

     Sample Input

    1
    ABCD EFGH
    even ABCI EFJK
    up ABIJ EFGH even

     Sample Output

    K is the counterfeit coin and it is light.

    思路:这道题数据量小,采用暴力遍历所有可能的24种情况,12枚硬币*2个情况(要么重了,要么轻了)=24 

      判断函数主要是四个判断:

    1. 字符出现在‘e’平等的情况下,则是合格的。
    2. 字符如果前面出现'u‘后面出现'd',说明这个字符是合格的。
    3. 如果在没有这个字符的情况下还出现重量不平等的情况,说明这个字符是合格。
    4. 这个字符可能未出现过,也说明合格。    
    #include <iostream>
    #include <string>
    #include <cstring>
    using namespace std;
    
    char strs[3][3][15];
    
    bool Judge(char point, int weight)
    {
        int tag = 0, flag = 0; //用于标记
        char ch;
        for (int j = 0; j < 3; j++)
        {
            ch = strs[j][2][0];
            tag = 0;
            for (int i = 0; i < 2; i++)
            {
                for (int k = 0; strs[j][i][k] != ''; k++)
                {
                    if (strs[j][i][k] == point)
                    {
                        tag = 1;
                        flag = 1;
                        if (ch == 'e')
                            return false;
                        int temp;
                        if (i == 0)//出现在天平左边 1代表重 2代表轻
                        {
                            if (ch == 'u')
                                temp = 1;
                            else
                                temp = 2;
                        }
                        else //出现在天平右边
                        {
                            if (ch == 'u')
                                temp = 2;
                            else
                                temp = 1;
                        }
                        if (temp != weight)//与字符重量不符合
                            return false;
                    }
                }
                if (tag)
                    break;
            }
            if (!tag && ch != 'e')//如果目标字符不在重量不对称的天平情况下
                return false;
        }
        return flag;//flag=0说明未出现过
    }
    
    int main()
    {
        int n;
        cin >> n;
        bool tag;
        string s[2] = {"heavy", "light"};
        while (n--)
        {
            for (int i = 0; i < 3; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    scanf("%s", strs[i][j]);
                }
            }
            for (int i = 0; i < 12; i++)
            {
                tag = false;
                for (int j = 0; j < 2; j++)
                {
                    if (Judge('A' + i, j + 1)) //1重 2轻
                    {
                        cout << (char)('A' + i) << " is the counterfeit coin and it is " << s[j] << '.' << endl;
                        tag = true;
                        break;
                    }
                }
                if (tag)
                    break;
            }
        }
        //system("pause");
    }
  • 相关阅读:
    linux的setup命令设置网卡和防火墙等
    在定时任务中慎用pause,否则造成弹窗没关闭,下一次任务不会成功执行
    删除指定文件路径下的所有文件及文件夹
    PHP遍历文件夹及子文件夹所有文件(此外还有飞递归的方法)
    解决wamp、vertrigo等集成环境安装后apache不能启动的问题
    【整理】Virtualbox中的网络类型(NAT,桥接等),网卡,IP地址等方面的设置
    linux IP 设置
    Linux常用命令大全
    在centos命令行下安装软件
    ubuntu12.04安装及配置过程详解1
  • 原文地址:https://www.cnblogs.com/dlvguo/p/12516688.html
Copyright © 2011-2022 走看看