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

本文介绍了一个经典的智力问题:如何在12个外观相同的球中,通过三次称重找出那个重量不同的球,并判断它是轻是重。文章给出了详细的解决步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 B
	if WR(A,B) = equal
		the abnormal ball should be in C
		2st Weighing {a1,a2,a3},{c1,c2,c3}
			if WR({a1,a2,a3}, {c1,c2,c3}) = equal
				c4 is the abnormal ball
			else
				the 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}) = up
				3st Weighing c1 c2
					if WR(c1,c2) = equal
						c3 is the abnormal ball
					else
						whatever the heavier one between c1 and c2 is the abnormal
					end
			end
	else
	the 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 statement
	Reconstruct: 
	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 before
	2st Weighing: X Z
		if WR(X,Z) = equal
			the abnormal ball is in Z
			3st Weighing: c1 a4
				if WR(c1,a4) = equal
					the abnormal ball is b4
				else
					the abnormal ball is a4
				end
		end
		if WR(X,Z) = up
			the abnormal ball should be among a1, a2 and b4
			3st Weighing: a1 a2
				if WR(a1,a2) = up
					a1 is the abnormal ball
				end
				if WR(a1,a2) = down
					a2 is the abnormal ball
				end
				if WR(a1,a2) = equal
					b4 is the abnormal ball
				end
		end
		if WR(X,Z)=down
			the abnormal ball should be between b1 and a4
			3st Weighing b1 c1
				if WR(b1,c1) = equal
					a4 is the abnormal ball
				else
					b1 is the abnormal ball
				end
		end
	end
Finally, 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值