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)有十几美元的旅行者银元。但是,只有11个硬币是真实的银元。一枚硬币是伪造的,即使它的颜色和大小与真实的银元没有区别。伪造的硬币的重量与其他硬币的重量不同,但是Sally不知道它比真实的硬币重还是轻。
幸好,莎莉有一位朋友向她借了一个非常准确的天平。朋友将允许Sally进行三次称重才能找到假币。例如,如果Sally互相称重两个硬币并且秤保持平衡,则她知道这两个硬币是正确的。现在,如果莎莉称重
真正的硬币中的一个相对于第三枚硬币,秤不平衡,然后Sally知道了第三枚硬币是伪造的,她可以根据放置在其上的天平是上升还是下降来分辨它是轻还是重。
通过仔细选择她的称重,Sally可以确保她会找到恰好三个称重的伪造硬币。
输入
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.
输入的第一行是整数n(n> 0),用于指定组数。 每个案例由三行输入组成,每一行称重。 萨莉(Sally)用字母A--L标识了每个硬币。 称量信息将由两串字母,然后是“up”,“down”或“even”之一给出。 第一个字符串代表左侧天平上的硬币; 第二个字符串代表右边的硬币。Sally总是在右侧天平上放置与左侧天平相同数量的硬币。第三个位置中的单词将告诉您天平右侧是向上,向下还是保持不变。
输出
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.
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);
}
}
Accepted