The Utils class is composed of some common methods:
java 代码
- package solve;
-
- import scale.Ball;
- import scale.Scale;
-
- import java.util.Iterator;
- import java.util.Set;
-
-
-
-
- public class Utils
- {
- public static Ball findGoodBall(Set ballSet)
- {
- for (Iterator it=ballSet.iterator(); it.hasNext();)
- {
- Ball ball = (Ball)it.next();
- if (ball.getStatus().equals(Ball.NORMAL)) return ball;
- }
-
- return null;
- }
-
- public static String convertToBallStatus(int scaleResult)
- {
- if (scaleResult == Scale.EVEN) return Ball.NORMAL;
- else if (scaleResult == Scale.HEAVIER) return Ball.HEAVIER;
- else return Ball.LIGHTER;
- }
-
- public static void markStatus(Set ballSet, String status)
- {
- for (Iterator it=ballSet.iterator(); it.hasNext();)
- {
- Ball ball = (Ball)it.next();
- if (!ball.getStatus().equals(Ball.NORMAL))
- {
- ball.setStatus(status);
- }
- }
- }
- }
The test class is as follows:
java 代码
- package solve;
-
- import junit.framework.TestCase;
-
- import java.util.Set;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.math.BigDecimal;
-
- import scale.Ball;
- import scale.Weight;
- import scale.Scale;
-
-
-
-
- public class SolverTest extends TestCase
- {
- public void test12Balls()
- {
- Set ballSet = createBallSet(12, 4, Ball.WEIGHT_HEAVIER);
- ProblemSolver solver = new ProblemSolver(ballSet);
- solver.findWeight();
-
- print(ballSet);
- System.out.println("scale usage=" + solver.getScale().getNumOfTrials());
- verifyResult(ballSet, solver.getScale());
- }
-
- public void test13Balls()
- {
- Set ballSet = createBallSet(13, 5, Ball.WEIGHT_HEAVIER);
- ProblemSolver solver = new ProblemSolver(ballSet);
- solver.findWeight();
-
- print(ballSet);
- System.out.println("scale usage=" + solver.getScale().getNumOfTrials());
- verifyResult(ballSet, solver.getScale());
- }
-
- public void testLoops()
- {
- int start = 500;
- int end = 1000;
- for (int i=start; i
- {
- for (int j=0; j
- {
- Set ballSet = createBallSet(i, j, Ball.WEIGHT_HEAVIER);
- ProblemSolver solver = new ProblemSolver(ballSet);
- solver.findWeight();
-
- System.out.println("num of balls=" + ballSet.size());
-
- System.out.println("scale usage=" + solver.getScale().getNumOfTrials());
- System.out.println("-------------------------------------------------");
- verifyResult(ballSet, solver.getScale());
- }
- }
- }
- private Set createBallSet(int size, int badBallIndex, Weight weight)
- {
- Set ballSet = new HashSet();
- for (int i=0; i
- {
- Weight weight1;
- if (i == badBallIndex) weight1 = weight;
- else weight1 = Ball.WEIGHT_NORNAL;
- Ball ball = new Ball("" + i, weight1);
- ballSet.add(ball);
- }
-
- return ballSet;
- }
-
- private void verifyResult(Set ballSet, Scale scale)
- {
- for (Iterator it=ballSet.iterator(); it.hasNext();)
- {
- Ball ball = (Ball)it.next();
- assertTrue(ball.validate());
- }
-
-
-
-
- BigDecimal aa = new BigDecimal(Math.log(2 * ballSet.size() + 3));
- BigDecimal bb = aa.divide(new BigDecimal(Math.log(3)), BigDecimal.ROUND_UP);
- int limit = (int)Math.ceil(bb.floatValue());
- assertTrue("limit=" + limit + ", trials=" + scale.getNumOfTrials(), limit >= scale.getNumOfTrials());
- }
-
- private void print(Set ballSet)
- {
- for (Iterator it=ballSet.iterator(); it.hasNext();)
- {
- Ball ball = (Ball)it.next();
- System.out.println(ball.toString());
- }
- }
- }
The testing is a little hard because of the recursion. Any change requires a full test. The testing checks whether the status flags we set matches the weights from the input and the times of weighings stays inside the limit.
分享到:
相关推荐
• 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 ...
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...
设有n 种不同面值的硬币,各硬币的面值存于数组T[1:n]中。现要用这些面值的硬币来找钱。可以使用的各种面值的硬币个数存于数组Coins[1:n]中。 对于给定的1≤n≤10,硬币面值数组T和可以使用的各种面值的硬币个...
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包
specific attention, each of which is addressed briefly below and then in more depth in the updated white paper: 1. Offering single-currency stablecoins in addition to the multi-currency coin. 2. ...
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 ...
根据提供的文件信息,可以看出这份文档并非关于《2019 Standard Catalog of World Coins, 2001-Date, 13th edition》的内容,而是关于一个特定软件或系统的功能需求文档。由于文档内容与标题及描述不相符,这里将...
本案例中的"coins.zip"文件聚焦于一个特定的编程挑战,即如何在给定条件下去寻找最轻且价值最大的硬币集合。让我们深入探讨这个话题。 首先,我们要理解问题的核心。假设有n枚硬币,它们的价值遵循一个幂律分布,即...
标题 "donut2_Fifa_coins_" 暗示了我们正在讨论的是一款与FIFA游戏相关的项目,可能是一个自动购买FIFA游戏内货币(Coins)的程序或工具。FIFA游戏是由Electronic Arts (EA)开发的一款全球知名的足球模拟游戏系列,...
ESXI虚拟化网络配置.doc
《基于Ogre游戏框架与Newton物理引擎的COINS项目解析》 在游戏开发领域,引擎的选择至关重要,Ogre是一款开源的3D渲染引擎,因其强大的图形处理能力而被广泛使用。而Newton物理引擎则提供了真实感的物理模拟,使得...
I = imread( C:\MATLAB701\toolbox\images\imdemos\coins.png ) imshow(I) figure, imhist(I,64)
动态规划题解coins.cpp
3. "As soon as he saw us, he picked up a long pipe which was covered with coins and opened one of the baskets." "picked up"在这里有两层含义:一是拿起或捡起,如"He picked up a long pipe which was ...
If you know how to program with Python and also know a little about probability, you’re ready to tackle Bayesian statistics. With this book, you'll learn how to solve statistical problems with Python...