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

本文介绍了一个用于检测一组球中异常重量球的算法实现。通过递归地将球分为几组并使用天平进行称重比较,算法能有效地找出较重或较轻的球。文章包含详细的Java代码示例及单元测试。
AI助手已提取文章相关产品:
The Utils class is composed of some common methods:

java 代码
 
  1. package solve;  
  2.   
  3. import scale.Ball;  
  4. import scale.Scale;  
  5.   
  6. import java.util.Iterator;  
  7. import java.util.Set;  
  8.   
  9. /** 
  10.  * utility class, shared common methods. 
  11.  */  
  12. public class Utils  
  13. {  
  14.     public static Ball findGoodBall(Set ballSet)  
  15.     {  
  16.         for (Iterator it=ballSet.iterator(); it.hasNext();)  
  17.         {  
  18.             Ball ball = (Ball)it.next();  
  19.             if (ball.getStatus().equals(Ball.NORMAL)) return ball;  
  20.         }  
  21.   
  22.         return null;  
  23.     }  
  24.   
  25.     public static String convertToBallStatus(int scaleResult)  
  26.     {  
  27.         if (scaleResult == Scale.EVEN) return Ball.NORMAL;  
  28.         else if (scaleResult == Scale.HEAVIER) return Ball.HEAVIER;  
  29.         else return Ball.LIGHTER;  
  30.     }  
  31.   
  32.     public static void markStatus(Set ballSet, String status)  
  33.     {  
  34.         for (Iterator it=ballSet.iterator(); it.hasNext();)  
  35.         {  
  36.             Ball ball = (Ball)it.next();  
  37.             if (!ball.getStatus().equals(Ball.NORMAL))  
  38.             {  
  39.                 ball.setStatus(status);  
  40.             }  
  41.         }  
  42.     }  
  43. }  

The test class is as follows:

java 代码
 
  1. package solve;  
  2.   
  3. import junit.framework.TestCase;  
  4.   
  5. import java.util.Set;  
  6. import java.util.HashSet;  
  7. import java.util.Iterator;  
  8. import java.math.BigDecimal;  
  9.   
  10. import scale.Ball;  
  11. import scale.Weight;  
  12. import scale.Scale;  
  13.   
  14. /** 
  15.  * 
  16.  */  
  17. public class SolverTest extends TestCase  
  18. {  
  19.     public void test12Balls()  
  20.     {  
  21.         Set ballSet = createBallSet(124, Ball.WEIGHT_HEAVIER);  
  22.         ProblemSolver solver = new ProblemSolver(ballSet);  
  23.         solver.findWeight();  
  24.   
  25.         print(ballSet);  
  26.         System.out.println("scale usage=" + solver.getScale().getNumOfTrials());  
  27.         verifyResult(ballSet, solver.getScale());  
  28.     }  
  29.   
  30.     public void test13Balls()  
  31.     {  
  32.         Set ballSet = createBallSet(135, Ball.WEIGHT_HEAVIER);  
  33.         ProblemSolver solver = new ProblemSolver(ballSet);  
  34.         solver.findWeight();  
  35.   
  36.         print(ballSet);  
  37.         System.out.println("scale usage=" + solver.getScale().getNumOfTrials());  
  38.         verifyResult(ballSet, solver.getScale());  
  39.     }  
  40.   
  41.     public void testLoops()  
  42.     {  
  43.         int start = 500;  
  44.         int end = 1000;  
  45.         for (int i=start; i
  46.         {  
  47.             for (int j=0; j
  48.             {  
  49.                 Set ballSet = createBallSet(i, j, Ball.WEIGHT_HEAVIER);  
  50.                 ProblemSolver solver = new ProblemSolver(ballSet);  
  51.                 solver.findWeight();  
  52.   
  53.                 System.out.println("num of balls=" + ballSet.size());  
  54.                 //print(ballSet);  
  55.                 System.out.println("scale usage=" + solver.getScale().getNumOfTrials());  
  56.                 System.out.println("-------------------------------------------------");  
  57.                 verifyResult(ballSet, solver.getScale());  
  58.             }  
  59.         }  
  60.     }  
  61.     private Set createBallSet(int size, int badBallIndex, Weight weight)  
  62.     {  
  63.         Set ballSet = new HashSet();  
  64.         for (int i=0; i
  65.         {  
  66.             Weight weight1;  
  67.             if (i == badBallIndex) weight1 = weight;  
  68.             else weight1 = Ball.WEIGHT_NORNAL;  
  69.             Ball ball = new Ball("" + i, weight1);  
  70.             ballSet.add(ball);  
  71.         }  
  72.   
  73.         return ballSet;  
  74.     }  
  75.   
  76.     private void verifyResult(Set ballSet, Scale scale)  
  77.     {  
  78.         for (Iterator it=ballSet.iterator(); it.hasNext();)  
  79.         {  
  80.             Ball ball = (Ball)it.next();  
  81.             assertTrue(ball.validate());  
  82.         }  
  83.   
  84.         // now computer the theoretic limit number of balls <=(3^n-3)/2, upper limit.  
  85.         // Number of balls  3  12  39  120  363  1092 3279 .....  
  86.         // Number of trials 2  3   4   5    6    7    8  
  87.         BigDecimal aa = new BigDecimal(Math.log(2 * ballSet.size() + 3));  
  88.         BigDecimal bb = aa.divide(new BigDecimal(Math.log(3)), BigDecimal.ROUND_UP);  
  89.         int limit = (int)Math.ceil(bb.floatValue());  
  90.         assertTrue("limit=" + limit + ", trials=" + scale.getNumOfTrials(), limit >= scale.getNumOfTrials());  
  91.     }  
  92.   
  93.     private void print(Set ballSet)  
  94.     {  
  95.         for (Iterator it=ballSet.iterator(); it.hasNext();)  
  96.         {  
  97.             Ball ball = (Ball)it.next();  
  98.             System.out.println(ball.toString());  
  99.         }  
  100.     }  
  101. }  

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.

您可能感兴趣的与本文相关内容

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
基于数据驱动的 Koopman 算子的递归神经网络模型线性化,用于纳米定位系统的预测控制研究(Matlab代码实现)内容概要:本文围绕“基于数据驱动的Koopman算子的递归神经网络模型线性化”展开,旨在研究纳米定位系统的预测控制问题,并提供完整的Matlab代码实现。文章结合数据驱动方法与Koopman算子理论,利用递归神经网络(RNN)对非线性系统进行建模与线性化处理,从而提升纳米级定位系统的精度与动态响应性能。该方法通过提取系统隐含动态特征,构建近似线性模型,便于后续模型预测控制(MPC)的设计与优化,适用于高精度自动化控制场景。文中还展示了相关实验验证与仿真结果,证明了该方法的有效性和先进性。; 适合人群:具备一定控制理论基础和Matlab编程能力,从事精密控制、智能制造、自动化或相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①应用于纳米级精密定位系统(如原子力显微镜、半导体制造设备)中的高性能控制设计;②为非线性系统建模与线性化提供一种结合深度学习与现代控制理论的新思路;③帮助读者掌握Koopman算子、RNN建模与模型预测控制的综合应用。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现流程,重点关注数据预处理、RNN结构设计、Koopman观测矩阵构建及MPC控制器集成等关键环节,并可通过更换实际系统数据进行迁移验证,深化对方法泛化能力的理解。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值