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 代码
- package scale;
-
-
-
- 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)
- {
-
- 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
- {
-
-
-
- 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);
-
-
-
-
- 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 + "]";
- }
-
-
- 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.
分享到:
相关推荐
• Handle the counterfeit coin problem: a classic puzzle that consists of finding a counterfeit coin in a beam balance among eight coins in only two turns Who This Book Is For Developers and ...
设有n 种不同面值的硬币,各硬币的面值存于数组T[1:n]中。现要用这些面值的硬币来找钱。可以使用的各种面值的硬币个数存于数组Coins[1:n]中。 对于给定的1≤n≤10,硬币面值数组T和可以使用的各种面值的硬币个...
Also at the bottom dropped a few coins. Years pass, but ... The Lost Watch II 3D Screensaver v1.0 build 3 Golden hours at the bottom of the river sparkling in the sun. Nobody does not initiate ...
-Count coins & dollar bills in an image after creating a currency counter -Find Legend of Zelda rupees using a pattern matching algorithm -Design a face swapping app -Discuss the mathematical theory &...
poj 3260 The Fewest Coins.md
The last game will have you running and jumping across platforms to collect coins and other exotic items. Throughout all these three games, you will create characters, make them move, and create some...
Ethereum coins to buy and reward the prize for convenience in term of speed and also reduce the problems which is unable to be controlled by the government. For example, lottery agents (intermediaries...
coins-security-1.0jar包
1. Offering single-currency stablecoins in addition to the multi-currency coin. 2. Enhancing the safety of the Libra payment system with a robust compliance framework. 3. Forgoing the future ...
which gives you coins in four types of cryptocurrency, such as: Ethereum, Litecoin, Dash, Dogecoin, which directly and without decoding the received hex with a series of tricks. Advanced has managed ...
假设有n枚硬币,它们的价值遵循一个幂律分布,即v(1, q, q^2, ..., q^n),其中q是某个正整数的底数。每枚硬币的重量统一为1单位。目标是找出总价值为Y的硬币集合,同时确保这个集合的总重量尽可能小。这个问题可以...
根据提供的文件信息,可以看出这份文档并非关于《2019 Standard Catalog of World Coins, 2001-Date, 13th edition》的内容,而是关于一个特定软件或系统的功能需求文档。由于文档内容与标题及描述不相符,这里将...
标题 "donut2_Fifa_coins_" 暗示了我们正在讨论的是一款与FIFA游戏相关的项目,可能是一个自动购买FIFA游戏内货币(Coins)的程序或工具。FIFA游戏是由Electronic Arts (EA)开发的一款全球知名的足球模拟游戏系列,...
ESXI虚拟化网络配置.doc
《基于Ogre游戏框架与Newton物理引擎的COINS项目解析》 在游戏开发领域,引擎的选择至关重要,Ogre是一款开源的3D渲染引擎,因其强大的图形处理能力而被广泛使用。而Newton物理引擎则提供了真实感的物理模拟,使得...
magic_coins1.py Chapter 3 Favourite sports. favourite_sports.py Furniture placeholder. furniture_placeholder.py A list of lists. list_of_lists.py A letter from Malcolm Dithering dithering_letter.py ...
I = imread( C:\MATLAB701\toolbox\images\imdemos\coins.png ) imshow(I) figure, imhist(I,64)
动态规划题解coins.cpp
标题与描述均提到了一个有趣的数学与编程问题:“假设有人民币10的n次方(变量值),输出1元,2元,5元的所有组合数”。这个问题涉及到的是组合数学中的硬币找零问题,通常在计算机科学领域,尤其是在算法设计与分析...