- 浏览: 80133 次
文章分类
最新评论
-
wodentt:
....为什么楼主都英文的博客
用java解决百度之星移动火柴的问题 part 1 -
wensonzhou86:
思路明确,代码清晰,通俗易懂
visit 淘宝面试题:如何充分利用多核CPU,计算很大的List中所有整数的和 -
carydeepbreathing:
代码比较干净
visit 淘宝面试题:如何充分利用多核CPU,计算很大的List中所有整数的和 -
meiowei:
yangguo 写道i don't think it is a ...
用java解决百度之星移动火柴的问题 part 2 -
yangguo:
i don't think it is a good way ...
用java解决百度之星移动火柴的问题 part 2
In the 1-ball case, if the status flag is already set, then we don't need to weight anymore. Otherwise, we need to find a good ball to compare.
In the 2-ball case, we need to check whether the status flags for both are set. If not, then we need two weighings.
In the above two cases, whenever we need to weigh, we need a good ball as the reference. In the 3-ball case, we don't need a good ball.
java 代码
- package solve;
- import scale.Ball;
- import scale.Scale;
- import java.util.Set;
- /**
- * 1-ball case: we need a good reference ball and at most one trial
- */
- public class OneBallCaseSolver
- {
- private Scale scale;
- private Set originalBallSet;
- public void findWeight(Set ballSet)
- {
- if (ballSet.size() != 1)
- throw new IllegalArgumentException("scale.Ball set size is not 1: size=" + ballSet.size());
- Ball ball = (Ball)ballSet.iterator().next();
- if (!ball.getStatus().equals(Ball.UNKNOWN)) return;
- // in order to figure it's good or bad, we need a good reference ball.
- Ball goodBall = Utils.findGoodBall(originalBallSet);
- if (goodBall == null)
- {
- throw new RuntimeException("can't findBallBall a good reference ball to use for the judgement");
- }
- // flag conversion
- int result = scale.weigh(ball, goodBall);
- ball.setStatus(Utils.convertToBallStatus(result));
- }
- public void setScale(Scale scale) { this.scale = scale; }
- public void setOriginalBallSet(Set originalBallSet) { this.originalBallSet = originalBallSet; }
- }
In the 2-ball case, we need to check whether the status flags for both are set. If not, then we need two weighings.
java 代码
- package solve;
- import scale.Scale;
- import scale.Ball;
- import java.util.Iterator;
- import java.util.Set;
- /**
- * 2-ball case: we need a good reference ball and one or two weighings.
- */
- public class TwoBallCaseSolver
- {
- private Scale scale;
- private Set originalBallSet;
- public void findWeight(Set ballSet)
- {
- if (ballSet.size() != 2)
- throw new IllegalArgumentException("scale.Ball set size is not 2: size=" + ballSet.size());
- Iterator it = ballSet.iterator();
- Ball balla = (Ball)it.next();
- Ball ballb = (Ball)it.next();
- Ball goodBall = Utils.findGoodBall(originalBallSet);
- int result = scale.weigh(balla, goodBall);
- balla.setStatus(Utils.convertToBallStatus(result));
- if (!balla.getStatus().equals(Ball.NORMAL))
- {
- ballb.setStatus(Ball.NORMAL);
- }
- else if (ballb.getStatus().equals(Ball.UNKNOWN))
- {
- result = scale.weigh(ballb, goodBall);
- ballb.setStatus(Utils.convertToBallStatus(result));
- }
- }
- public void setScale(Scale scale) { this.scale = scale; }
- public void setOriginalBallSet(Set originalBallSet) { this.originalBallSet = originalBallSet; }
- }
In the above two cases, whenever we need to weigh, we need a good ball as the reference. In the 3-ball case, we don't need a good ball.
java 代码
- package solve;
- import scale.Scale;
- import scale.Ball;
- import java.util.Set;
- import java.util.Iterator;
- /**
- * 3-ball case: we need two trials if all of the balls are not marked, otherwise
- * we need only one trial.
- */
- public class ThreeBallCaseSolver
- {
- private Scale scale;
- public void findWeight(Set ballSet)
- {
- if (ballSet.size() != 3)
- throw new IllegalArgumentException("scale.Ball set size is not 3: size=" + ballSet.size());
- // In the 3-ball case, we can figure out which one is good.
- // Here, we assume there is only one bad ball.
- Iterator it = ballSet.iterator();
- Ball balla = (Ball)it.next();
- Ball ballb = (Ball)it.next();
- Ball ballc = (Ball)it.next();
- if (balla.getStatus().equals(Ball.UNKNOWN)) // This means we start with 3 balls
- {
- // In this case we need two trials
- int result = scale.weigh(balla, ballb);
- if (result == Scale.EVEN)
- {
- // This means balla and ballb are good, ballc is bad.
- balla.setStatus(Ball.NORMAL);
- ballb.setStatus(Ball.NORMAL);
- if (scale.weigh(balla, ballc) == Scale.HEAVIER)
- ballc.setStatus(Ball.LIGHTER);
- else
- ballc.setStatus(Ball.HEAVIER);
- }
- else //if (result == Scale.HEAVIER)
- {
- // This means ballc is good
- ballc.setStatus(Ball.NORMAL);
- int result1 = scale.weigh(balla, ballc);
- if (result1 == Scale.EVEN) // a is also good
- {
- balla.setStatus(Ball.NORMAL);
- if (result == Scale.HEAVIER)
- ballb.setStatus(Ball.LIGHTER);
- else
- ballb.setStatus(Ball.HEAVIER);
- }
- else if (result1 == Scale.HEAVIER) // b is good, a is heavier
- {
- balla.setStatus(Ball.HEAVIER);
- ballb.setStatus(Ball.NORMAL);
- }
- else
- {
- balla.setStatus(Ball.LIGHTER);
- ballb.setStatus(Ball.NORMAL);
- }
- }
- }
- else // This means we start with more than 3 balls and markWeight is marked.
- {
- // In this case we need only 1 trial with the marked info.
- // At this stage, all 3 balls are marked with either lighter or heavier.
- // From the Pigeon Principle, we can choose two balls with the same mark.
- Ball w1;
- Ball w2;
- Ball w3;
- if (balla.getStatus().equals(ballb.getStatus()))
- {
- w1 = balla;
- w2 = ballb;
- w3 = ballc;
- }
- else if (balla.getStatus().equals(ballc.getStatus()))
- {
- w1 = balla;
- w2 = ballc;
- w3 = ballb;
- }
- else if (ballb.getStatus().equals(ballc.getStatus()))
- {
- w1 = ballb;
- w2 = ballc;
- w3 = balla;
- }
- else
- {
- throw new RuntimeException("The balls are not marked properly");
- }
- int result = scale.weigh(w1, w2);
- if (result == Scale.EVEN)
- {
- w1.setStatus(Ball.NORMAL);
- w2.setStatus(Ball.NORMAL);
- }
- else if (result == Scale.HEAVIER)
- {
- w3.setStatus(Ball.NORMAL);
- if (w1.getStatus().equals(Ball.HEAVIER)) // heavier is bad
- {
- w2.setStatus(Ball.NORMAL);
- }
- else // lighter is bad
- {
- w1.setStatus(Ball.NORMAL);
- }
- }
- else
- {
- w3.setStatus(Ball.NORMAL);
- if (w1.getStatus().equals(Ball.HEAVIER))
- {
- w1.setStatus(Ball.NORMAL);
- }
- else
- {
- w2.setStatus(Ball.NORMAL);
- }
- }
- }
- }
- public void setScale(Scale scale) { this.scale = scale; }
- }
发表评论
-
Revisit 一道应聘智力题的编程求解, Einstein puzzle
2010-03-09 00:36 1451Another brain teaser, titled 一道 ... -
Another Google question - drop glassballs from a building 5
2007-05-13 04:01 1080Here are some results are the t ... -
Another Google question - drop glassballs from a building 4
2007-05-13 03:42 1539The test cases are divided into ... -
Another Google question - drop glassballs from a building 3
2007-05-13 03:33 1096The next step is to find the ac ... -
Another Google question - drop glassballs from a building 2
2007-05-13 03:02 1110Here is the code following the ... -
Another Google question - drop glassballs from a building 1
2007-05-12 04:43 1209Here is the question: 原题: ... -
Given n coins and a pan balance,find the only counterfeit 4
2007-02-27 05:54 1119The Utils class is composed of ... -
Given n coins and a pan balance,find the only counterfeit 2
2007-02-27 05:29 1109Now it comes to the Scale class ... -
Given n coins and a pan balance, find the only counterfeit 1
2007-02-27 04:49 1340The problem is: There is one ... -
A Google's Interview Question - GLAT #20 series 4
2007-02-08 02:36 1107The entire class is as follows: ... -
A Google's Interview Question - GLAT #20 series 3
2007-02-08 02:36 1070The roadmap to compute all fixe ... -
A Google's Interview Question - GLAT #20 series 2
2007-02-08 02:24 1074Now, we can deduct the recursio ... -
A Google's Interview Question - GLAT #20 series 1
2007-02-08 02:22 1162Question: Consider a function ...
相关推荐
• 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包
《基于Ogre游戏框架与Newton物理引擎的COINS项目解析》 在游戏开发领域,引擎的选择至关重要,Ogre是一款开源的3D渲染引擎,因其强大的图形处理能力而被广泛使用。而Newton物理引擎则提供了真实感的物理模拟,使得...
Libra2.0白皮书2020年4月...3. Forgoing the future transition to a permissionless system while maintaining its key economic properties. 4. Building strong protections into the design of the Libra Reserve.
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 ...
标题 "donut2_Fifa_coins_" 暗示了我们正在讨论的是一款与FIFA游戏相关的项目,可能是一个自动购买FIFA游戏内货币(Coins)的程序或工具。FIFA游戏是由Electronic Arts (EA)开发的一款全球知名的足球模拟游戏系列,...
根据提供的文件信息,可以看出这份文档并非关于《2019 Standard Catalog of World Coins, 2001-Date, 13th edition》的内容,而是关于一个特定软件或系统的功能需求文档。由于文档内容与标题及描述不相符,这里将...
本案例中的"coins.zip"文件聚焦于一个特定的编程挑战,即如何在给定条件下去寻找最轻且价值最大的硬币集合。让我们深入探讨这个话题。 首先,我们要理解问题的核心。假设有n枚硬币,它们的价值遵循一个幂律分布,即...
《PyPI官网下载:深入理解py_aiger_coins-3.3.0-py3-none-any.whl》 PyPI(Python Package Index)是Python开发者的重要资源库,它提供了丰富的Python软件包,使得开发者能够方便地下载、安装和分享代码。在本篇中...
ESXI虚拟化网络配置.doc
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 ...