百练OJ:1013:Counterfeit Dollar(假币)
題目描述
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
總結
以上是生活随笔為你收集整理的百练OJ:1013:Counterfeit Dollar(假币)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leetcode7 :整数反转(JAVA
- 下一篇: 天翼云认证--大纲介绍