首页 > 专题系列 > Java解POJ > POJ 1013 Counterfeit Dollar [解题报告] Java
2013
11-08

POJ 1013 Counterfeit Dollar [解题报告] Java

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.

输入:

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.

输出:

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.*;
public class Main {
public static void main(String[] args) {
 Scanner cin=new Scanner(System.in);
 int N=cin.nextInt();
 while(N-->0){
   plate[][] p = new plate[4][4];
   for(int i=1;i<=3;i++)
    for(int j=1;j<=3;j++)
     p[i][j]=new plate(cin.next());
   char t='A';
   while(t<='L'){
    int count=0;
    for(int i=1;i<=3;i++)
     if(judge(p[i][1],p[i][2],t,true).equals(p[i][3].get_s()))
      count++;
    if(count==3){
     System.out.println(t+" is the counterfeit coin and it is heavy.");
     break;
    }
    count=0;
    for(int i=1;i<=3;i++)
     if(judge(p[i][1],p[i][2],t,false).equals(p[i][3].get_s()))
      count++;
    if(count==3){
     System.out.println(t+" is the counterfeit coin and it is light.");
     break;
    }
    t++;
   }
  
 }
}
public static String judge(plate a,plate b,char c,boolean heavy){
   if(!a.contain(c) && !b.contain(c))
    return "even";
   else{
    if(heavy){
     if(a.contain(c))
      return "up";
     else
      return "down";
    }
    else{
     if(a.contain(c))
      return "down";
     else
      return "up";
    }
   }
 }
}

class plate{
 private String s;
 boolean contain(char a){
   if(s.contains(String.valueOf(a)))
    return true;
   else
    return false;
 }
 boolean equal(String t){
   if(s.equals(t))
    return true;
   else
    return false;
 }
 plate(String t){
   s=t;
 }
 String get_s(){
   return s;
 }
}
   
(2)方法(二)
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStreamReader;

 public class Main {

     public static void main(String[] args) throws NumberFormatException,
             IOException {
         BufferedReader read = new BufferedReader(new InputStreamReader(
                 System.in));
         int t = Integer.parseInt(read.readLine());
         String[][] bijiao;
         boolean[] coin;
         int[] hl;
         int fault;
         for (int i = 0; i < t; i++) {
             fault = -1;
             bijiao = new String[3][];
             hl = new int[12];
             coin = new boolean[12];
             for (int j = 0; j < 3; j++) {
                 bijiao[j] = read.readLine().split(" ");
                 if (bijiao[j][2].equals("even")) {
                     for (int k = 0; k < bijiao[j][0].length(); k++) {
                         coin[bijiao[j][0].charAt(k) - 'A'] = true;
                         coin[bijiao[j][1].charAt(k) - 'A'] = true;
                     }
                 } else {
                     for (int k = 0; k < 12; k++) {
                         if (bijiao[j][0].indexOf((char) ('A' + k)) == -1
                                 && bijiao[j][1].indexOf((char) ('A' + k)) == -1) {
                             coin[k] = true;
                         }
                     }
                 }
             }
             for (int j = 0; j < 3; j++) {
                 if (bijiao[j][2].equals("up")) {
                     for (int k = 0; k < bijiao[j][0].length(); k++) {
                         if (hl[bijiao[j][0].charAt(k) - 'A'] != 1) {
                             hl[bijiao[j][0].charAt(k) - 'A'] = -1;
                         } else {
                             coin[bijiao[j][0].charAt(k) - 'A'] = true;
                         }
                         if (hl[bijiao[j][1].charAt(k) - 'A'] != -1) {
                             hl[bijiao[j][1].charAt(k) - 'A'] = 1;
                         } else {
                             coin[bijiao[j][1].charAt(k) - 'A'] = true;
                         }
                     }
                 } else if (bijiao[j][2].equals("down")) {
                     for (int k = 0; k < bijiao[j][0].length(); k++) {
                         if (hl[bijiao[j][0].charAt(k) - 'A'] != -1) {
                             hl[bijiao[j][0].charAt(k) - 'A'] = 1;
                         } else {
                             coin[bijiao[j][0].charAt(k) - 'A'] = true;
                         }
                         if (hl[bijiao[j][1].charAt(k) - 'A'] != 1) {
                             hl[bijiao[j][1].charAt(k) - 'A'] = -1;
                         } else {
                             coin[bijiao[j][1].charAt(k) - 'A'] = true;
                         }
                     }
                 }
             }
             for (int j = 0; j < 12; j++) {
                 if (!coin[j]) {
                     fault = j;
                     break;
                 }
             }
             if (fault != -1 && hl[fault] == 1) {
                 System.out.println((char) ('A' + fault)
                         + " is the counterfeit coin and it is light.");
             } else if (fault != -1 && hl[fault] == -1) {
                 System.out.println((char) ('A' + fault)
                         + " is the counterfeit coin and it is heavy.");
             }
         }
}
}

  1. 问题3是不是应该为1/4 .因为截取的三段,无论是否能组成三角形, x, y-x ,1-y,都应大于0,所以 x<y,基础应该是一个大三角形。小三角是大三角的 1/4.

  2. 很高兴你会喜欢这个网站。目前还没有一个开发团队,网站是我一个人在维护,都是用的开源系统,也没有太多需要开发的部分,主要是内容整理。非常感谢你的关注。

  3. I like your publish. It is great to see you verbalize from the coronary heart and clarity on this essential subject matter can be easily noticed.