Abnormal Ball, I found you die: Easy Algorithms in pseudo-code

来源:互联网 发布:幸运转盘的php源码 编辑:程序博客网 时间:2024/06/05 10:15

1. Description:

The question this article focuses on is a very old intellectual question. However, the thoughts and algorithms solving it is still worth thinking.


2. Question:

There are 12 identical balls in the desk except for one abnormal ball inside having different weight. Besides, apart from the 12 balls, there is scale on the desk. But the scale is without counterweight. So, this scale can only be used for telling which one is heavier or lighter between two items. The question is how to locate the abnormal ball by 3 times weighing for those balls.

Remark: in this question, we do not know whether the abnormal ball is lighter or heavier than the normal.


3. Solution:

Notation: we denote WR as the weighing relationship between any two different items

In this place,

if A is equal to B, we denote WR(A,B) = equal;

if A is heavier than B, we denote WR(A,B) = down;

if A is lighter than B, we denote WR(A,B) = up;

First we need to divide those 12 balls into 3 groups: A = {a1,a2,a3,a4}; B = {b1,b2,b3,b4}; C = {c1,c2,c3,c4};1st Weighing: A Bif WR(A,B) = equalthe abnormal ball should be in C2st Weighing {a1,a2,a3},{c1,c2,c3}if WR({a1,a2,a3}, {c1,c2,c3}) = equalc4 is the abnormal ballelsethe abnormal ball should be among {c1,c2,c3}note down the WR of {a1,a2,a3} and {c1,c2,c3}Here, we consider the case: the abnormal ball is heavier,i.e. WR({a1,a2,a3},{c1,c2,c3}) = up3st Weighing c1 c2if WR(c1,c2) = equalc3 is the abnormal ballelsewhatever the heavier one between c1 and c2 is the abnormalendendelsethe abnormal ball should be among {a1,a2,a3,a4,b1,b2,b3,b4}note down the WR of A = {a1,a2,a3,a4} and B = {b1,b2,b3,b4}in this place we denote the case is WR(A,B) = up for convenient statementReconstruct: divide those balls into three groups including X = {a1,a2,b1}, Y = {a3,b2,b3}, and Z = {b4,a4,c1}; c1 is a normal ball as checked before2st Weighing: X Zif WR(X,Z) = equalthe abnormal ball is in Z3st Weighing: c1 a4if WR(c1,a4) = equalthe abnormal ball is b4elsethe abnormal ball is a4endendif WR(X,Z) = upthe abnormal ball should be among a1, a2 and b43st Weighing: a1 a2if WR(a1,a2) = upa1 is the abnormal ballendif WR(a1,a2) = downa2 is the abnormal ballendif WR(a1,a2) = equalb4 is the abnormal ballendendif WR(X,Z)=downthe abnormal ball should be between b1 and a43st Weighing b1 c1if WR(b1,c1) = equala4 is the abnormal ballelseb1 is the abnormal ballendendendFinally, we can obtain the abnormal ball within 3 moves.
Tips: 

There is unique abnormal ball among the 12 balls. 

The weight relationship among balls does not change during procedure. 

(Applied Math is dreaming of coding... ...)

Author: Clarence

0 0
原创粉丝点击