POJ 1013 Counterfeit Dollar

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. 

Source

 
    题目大致意思:就是有好几枚硬币,然后其中有一枚是假的,给出三组称量数据,要你判断哪一枚硬币是假的,是重了还是轻了。
    分析:
    1.一开始我们都不知道哪一枚硬币是假的,所以对每一枚硬币都保持怀疑
    2.如果两组硬币称量得出的结果是“even”,那么这八枚硬币肯定是真的,给予十分的信任。(因为只有一枚硬币是假的,八枚硬币质量相等,那么问题肯定不会出在这里)
    3.如果出现“up”,即右边的轻了,那么左边的硬币除了十分信任的都要++,表示怀疑次数+1,右边的除了十分信任的以外都--,表示怀疑次数+1(绝对值)
    4.如果出现“down”,做法同上,不过方向反一下就好
    5.最后我们找到怀疑次数(绝对值)最多的那一枚硬币,判断是小于零还是大于零,对应是重了还是轻了
 
代码如下:
#include <iostream>
#include <string>

using namespace std;

int main()
{
    int n;
    int coin[11];
    string str1, str2, judge;
    while (cin >> n)
    {
        for (int i = 0; i < n; i++)
        {
            memset(coin, 0, sizeof(coin));    //初始任何一枚银币都值得怀疑
            for (int j = 0; j < 3; j++)
            {
                cin >> str1 >> str2 >> judge;
                if (judge == "even")          //因为只有一枚银币不同 如果重量相同 那么两边的银币都是真的
                {
                    for (int k = 0; k < str1.size(); k++) coin[str1[k] - 'A'] = 10;
                    for (int k = 0; k < str2.size(); k++) coin[str2[k] - 'A'] = 10;
                }
                else if (judge == "up")       //右边的轻一些
                {
                    for (int k = 0; k < str1.size(); k++)
                    {
                        if (coin[str1[k] - 'A'] == 10) continue;
                        coin[str1[k] - 'A']++;
                    }
                    for (int k = 0; k < str2.size(); k++)
                    {
                        if (coin[str2[k] - 'A'] == 10) continue;
                        coin[str2[k] - 'A']--;
                    }
                }
                else if (judge == "down")    //右边的重一些
                {
                    for (int k = 0; k < str1.size(); k++)
                    {
                        if (coin[str1[k] - 'A'] == 10) continue;
                        coin[str1[k] - 'A']--;
                    }
                    for (int k = 0; k < str2.size(); k++)
                    {
                        if (coin[str2[k] - 'A'] == 10) continue;
                        coin[str2[k] - 'A']++;
                    }
                }
            }
            int p, max;
            max = -99;
            for (int k = 0; k < 11; k++)         //找到怀疑次数最多的一个
            {
                if (coin[k] == 10) continue;
                if (max < abs(coin[k]))
                {
                    p = k;
                    max = abs(coin[k]);
                }
            }
            char ch = p + 'A';
            if (coin[p] > 0) cout << ch << " is the counterfeit coin and it is heavy. " << endl;
            else cout << ch << " is the counterfeit coin and it is light. " << endl;
        }
    }
    return 0;
}

 

posted @ 2017-12-08 09:16  念你成疾  阅读(200)  评论(0编辑  收藏  举报