Bailian OJ: 1013: Counterfeit Dollar (counterfeit currency)

Title 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.

Sally Jones has a dozen traveller silver dollars. However, only 11 coins are real silver dollars. A coin is forged, even if its color and size are no different from real silver dollars. The weight of the counterfeit coin is different from the weight of other coins, but Sally does not know whether it is heavier or lighter than the real coin.
Fortunately, a friend of Sally borrowed a very accurate balance from her. The friend will allow Sally to weigh three times to find the counterfeit currency. For example, if Sally weighs two coins against each other and the scale is balanced, she knows that the two coins are correct. Now, if Sally weighs one of the
real coins relative to the third coin, the scale is not balanced, and then Sally knows that the third coin is a counterfeit, she can depend on whether the balance placed on it rises or falls. Distinguish whether it is light or heavy.
By carefully choosing her weighing, Sally can ensure that she will find exactly three weighed counterfeit coins.

enter

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.

The first line of input is an integer n (n> 0), which is used to specify the number of groups. Each case consists of three lines of input, and each line is weighed. Sally uses the letters A--L to identify each coin. The weighing information will be given by two strings of letters followed by one of "up", "down" or "even". The first string represents the coin on the left balance; the second string represents the coin on the right. Sally always puts the same number of coins on the balance on the right as on the balance on the left. The word in the third position will tell you whether the right side of the balance is up, down, or remains the same.

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.

In each case, the output will identify the counterfeit coin by letters and tell it if 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. 

Problem-solving code

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        while (n > 0) {
            n--;

            String[] e1 = new String[3];
            e1[0] = scanner.next();
            e1[1] = scanner.next();
            e1[2] = scanner.next();
            String[] e2 = new String[3];
            e2[0] = scanner.next();
            e2[1] = scanner.next();
            e2[2] = scanner.next();
            String[] e3 = new String[3];
            e3[0] = scanner.next();
            e3[1] = scanner.next();
            e3[2] = scanner.next();

            for (int i = 'A'; i <= 'L'; i++) {
                //每次选出一个质量不相等的那个,先假设它质量是低的,再假设它质量是高的。
                char a = (char) i;
                if (isRight(e1, a, 0) & isRight(e2, a, 0) && isRight(e3, a, 0)) {

                    System.out.println(a + " is the counterfeit coin and it is light.");
                    break;
                } else if (isRight(e1, a, 1) & isRight(e2, a, 1) && isRight(e3, a, 1)) {
                    System.out.println(a + " is the counterfeit coin and it is heavy.");
                    break;
                }

            }
        }
    }

    private static boolean isRight(String[] e, char album, int i) {
        String result = "even";
        String c = String.valueOf(album);

        if (e[0].contains(c) || e[1].contains(c)) {
            if (i == 0) {
                //包含的那一边是轻的
                if (e[0].contains(c)) {
                    //左边轻,右边重
                    result = "down";

                } else {
                    //右边轻
                    result = "up";
                }
            }
            if (i == 1) {
                //包含的那一边是重的
                if (e[0].contains(c)) {
                    //左边重
                    result = "up";
                } else {
                    //右边重
                    result = "down";
                }
            }

        }


        return e[2].equals(result);
    }


}

Problem solving result

Accepted

Guess you like

Origin blog.csdn.net/Kangyucheng/article/details/107602372