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
- Abnormal Ball, I found you die: Easy Algorithms in pseudo-code
- Pseudo code for In-class QuestionExample 3: Write pseudo code that performs the following: Ask a use
- 自己实现全排列:I found some old code were good:We should think in a reverse way
- <LeetCode>Algorithms-Easy
- I LOVE YOU IN EVERY DIMENSION
- LightOj 1054 Efficient Pseudo Code
- found duplicated code in this file
- Pseudo-variables in Watch Windows
- Keep alive till I die
- Maybe I Will Die Tomorrow
- hollister soldes the police and hospital logistics park in the lounge driver found 812 small ball
- UVA 11198 (in easy way that i learnt from others)
- Think before you code, Virtual Functions in C++
- Think before you code, Virtual Functions in C++
- [cn]伪代码 --- pseudo code[en]
- Pre-IPL RAM Stage pseudo C code
- Light oj 1054 - Efficient Pseudo Code
- Light OJ 1054 - Efficient Pseudo Code
- oracle体系结构的学习
- 二分匹配
- 传感器概述
- CentOS 6.X 安装 EPEL 源
- 网络编程_网络模型概述和图解
- Abnormal Ball, I found you die: Easy Algorithms in pseudo-code
- KMP算法next数组生成中k=next[k]解释
- CentOS Apache配置详解
- 解决Ubuntu下耳机没有声音的问题
- 浅谈数据库范式
- Two-phase commit(2PC) 协议介绍/以及疑问
- 新手常见杨辉三角,斐波那契数列,百元百鸡求法
- Discuz!开发之模板制作CSS扩展规范与语法规范
- C++重载之转换构造函数与类型转换函数