Given n coins and a pan balance, find the only counterfeit 1 博客分类: algorithms AccessUP
程序员文章站
2024-03-07 12:56:15
...
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 *.
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.
Now, let's look at the Ball class.
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.
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 *.
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 代码
- package scale;
- /**
- * The only behaviors we care are the add and compare methods.
- */
- public class Weight implements Comparable
- {
- private int amount;
- public Weight(int amount)
- {
- this.amount = amount;
- }
- public void add(Weight weight)
- {
- if (weight == null) throw new IllegalArgumentException("weight is null");
- amount += weight.amount;
- }
- public boolean equals(Object obj)
- {
- if (!(obj instanceof Weight)) return false;
- Weight weight = (Weight)obj;
- return this.amount == weight.amount;
- }
- public int hashCode() { return amount; }
- public int compareTo(Object obj)
- {
- // this is arguable
- if (!(obj instanceof Weight)) return -1;
- Weight weight = (Weight)obj;
- if (this.amount == weight.amount) return 0;
- else
- {
- return this.amount > weight.amount ? 1 : -1;
- }
- }
- public String toString() { return "(weight=" + amount + ")"; }
- }
Now, let's look at the Ball class.
java 代码
- package scale;
- public class Ball
- {
- // These constants are totally ordered(compared), can be sum'd up.
- // So if we want replace this type, we have to have these two features.
- // Also this is the input: scale.Scale can use the sum method only.
- public static final Weight WEIGHT_HEAVIER = new Weight(1);
- public static final Weight WEIGHT_LIGHTER = new Weight(-1);
- public static final Weight WEIGHT_NORNAL = new Weight(0);
- // These flags can be of any type.
- // This is the result. Though this is 1-1 mapping to the enums in the scale.Scale class,
- // we want to keep it seperate since this is our output.
- public static final String NORMAL = "Normal";
- public static final String HEAVIER = "Heavier";
- public static final String LIGHTER = "Lighter";
- public static final String UNKNOWN = "unknown";
- private String id = "";
- private Weight weight = WEIGHT_NORNAL;
- private String status = UNKNOWN;
- public Ball(String id, Weight weight)
- {
- this.id = id;
- this.weight = weight;
- }
- public String toString()
- {
- return "Ball[id=" + id + " " + weight.toString() + " status=" + status + "]";
- }
- // make this package level so solvers don't have access to cheat on this.
- Weight getWeight() { return weight; }
- public String getStatus() { return status; }
- public void setStatus(String status) { this.status = status; }
- public boolean validate()
- {
- return (this.weight.equals(WEIGHT_HEAVIER) && status.equals(HEAVIER)) ||
- (this.weight.equals(WEIGHT_LIGHTER) && status.equals(LIGHTER)) ||
- (this.weight.equals(WEIGHT_NORNAL) && status.equals(NORMAL));
- }
- }
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.