Given n coins and a pan balance, find the only counterfeit 1

The problem is:

There is one counterfeit coin among 12 coins. It is unknown whether the counterfeit is lighter or heavier than a genuine coin(all genuine coins weigh the same). Using 3 weighings on a pan balance, how can the counterfeit be identified and determined to be lighter or heavier than a genuine coin.
 
To generalize this, given n coins with only one counterfeit, how many weighings are needed in order to find the counterfeit and determine whether it's lighter or heavier?

I first encountered this problem in the book "The USSR Olympiad problem book"(from Dover, check amazon site). There is also a Chinese translation from Hong Kong.

It's the #6 problem in the book. The 12 and 13-coin case is well known. The complete solution is given in the book. Here I am more interested in the programming solution.

Apparently, we have balls and a scale, so naturally, we should create two classes. Let's look at the Scale class. The functionality of a scale is to weigh both sides, so we should have a weigh() function, that could take two sets of balls, or just two balls. Also we want to count how many times we use the scale, so we have an internal counter for this. The last thing is to flag whether both sides are equal or not, therefore we need some constants to indicate whether the first set is heavier/lighter/equal to the second set.

Then we definitely need a Ball class. In this class, we need a weight field for the problem. A simple approach is to use an int type to indicate whether it's normal/heavier/lighter. However, all we need for this type is to be able to add up weights in a set of balls and compare weights from two sets of balls on the scale.  So we could encapsulate these in a class, called Weight.

java 代码
 
  1. package scale;  
  2. /** 
  3.  * The only behaviors we care are the add and compare methods. 
  4.  */  
  5. public class Weight implements Comparable  
  6. {  
  7.     private int amount;  
  8.   
  9.     public Weight(int amount)  
  10.     {  
  11.         this.amount = amount;  
  12.     }  
  13.   
  14.     public void add(Weight weight)  
  15.     {  
  16.         if (weight == nullthrow new IllegalArgumentException("weight is null");  
  17.   
  18.         amount += weight.amount;  
  19.     }  
  20.   
  21.     public boolean equals(Object obj)  
  22.     {  
  23.         if (!(obj instanceof Weight)) return false;  
  24.   
  25.         Weight weight = (Weight)obj;  
  26.         return this.amount == weight.amount;  
  27.     }  
  28.   
  29.     public int hashCode() { return amount; }  
  30.   
  31.     public int compareTo(Object obj)  
  32.     {  
  33.         // this is arguable  
  34.         if (!(obj instanceof Weight)) return -1;  
  35.   
  36.         Weight weight = (Weight)obj;  
  37.         if (this.amount == weight.amount) return 0;  
  38.         else  
  39.         {  
  40.             return this.amount > weight.amount ? 1 : -1;  
  41.         }  
  42.     }  
  43.   
  44.     public String toString() { return "(weight=" + amount + ")"; }  
  45. }  

Now, let's look at the Ball class.

java 代码
 
  1. package scale;  
  2.   
  3. public class Ball  
  4. {  
  5.     // These constants are totally ordered(compared), can be sum'd up.  
  6.     // So if we want replace this type, we have to have these two features.  
  7.     // Also this is the input: scale.Scale can use the sum method only.  
  8.     public static final Weight WEIGHT_HEAVIER = new Weight(1);  
  9.     public static final Weight WEIGHT_LIGHTER = new Weight(-1);  
  10.     public static final Weight WEIGHT_NORNAL  = new Weight(0);  
  11.   
  12.     // These flags can be of any type.  
  13.     // This is the result. Though this is 1-1 mapping to the enums in the scale.Scale class,  
  14.     // we want to keep it seperate since this is our output.  
  15.     public static final String NORMAL  = "Normal";  
  16.     public static final String HEAVIER = "Heavier";  
  17.     public static final String LIGHTER = "Lighter";  
  18.     public static final String UNKNOWN = "unknown";  
  19.   
  20.     private String id = "";  
  21.     private Weight weight = WEIGHT_NORNAL;  
  22.   
  23.     private String status = UNKNOWN;  
  24.   
  25.     public Ball(String id, Weight weight)  
  26.     {  
  27.         this.id = id;  
  28.         this.weight = weight;  
  29.     }  
  30.   
  31.     public String toString()  
  32.     {  
  33.         return "Ball[id=" + id + " " + weight.toString() + " status=" + status + "]";  
  34.     }  
  35.   
  36.     // make this package level so solvers don't have access to cheat on this.  
  37.     Weight getWeight() { return weight; }  
  38.   
  39.     public String getStatus() { return status; }  
  40.     public void setStatus(String status) { this.status = status; }  
  41.   
  42.     public boolean validate()  
  43.     {  
  44.         return (this.weight.equals(WEIGHT_HEAVIER) && status.equals(HEAVIER)) ||  
  45.                (this.weight.equals(WEIGHT_LIGHTER) && status.equals(LIGHTER)) ||  
  46.                (this.weight.equals(WEIGHT_NORNAL) && status.equals(NORMAL));  
  47.     }  
  48. }  

Here I treat the weight as input, and create another field status to detect the ball's weight. This is more like a flag field that we will set later. It's an output. Note that the getWeight() method is package access, so only the Scale class will use this knowledge.

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.用c语音写
04-03
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值