10 small balls with a balance to find the one with different weights.

Source: Internet
Author: User

Last week was an algorithm topic, the topic is: There are 10 small balls, the appearance of the same, of which 1 balls and other 9 ball weight is different, please use the balance to find the minimum number of times the weight of the ball.

Problem-solving ideas: In fact, this is a very typical use of the partition algorithm example, because do not know this particular ball is heavier than other balls or light, so we can not simply use the binary method to solve, so I choose 3 for the mold.

Problem Solving steps:

1. Divide 10 balls into 4 arrays, namely A[A1, A2, A3],b[b1, B2, B3], c[c1, C2, C3, D[d1]; Suppose that the weight of the ball is N, and any other ordinary ball is m;

2, first take A and B, a and C said, if A==b && a== C, then directly get the result n = d1; otherwise we can get two results, the first n in [a| b| C], the second one is n is heavier or lighter than M.

Description

If a > B && a > C, then N in A,n > m

If a > B && a = = C, then n in b,n < m

If a > B && a < C, this situation does not exist

if A = = B && A > C, then N in C, n < m

if A = = B && A = = C, this situation we have obtained the result

if A = = B && A < C, then n in C, n > M

If a < B && a < C, then N in a,n < m

If a < B && a = = C, then n in B,n > m

If a < B && a > C, this situation does not exist

3. We get n in [a| b| C] After taking the first two balls in a, B, C, it is known that n is heavier or lighter than m, so you can get n directly after the call. Suppose N in A

Description

If a1 > A2 && n > M, then n = A1

If a1 > A2 && N < m, then n = A2

If a1 = = A2, then n = A3

If A1 < A2 && n > M, then n = A2

If A1 < A2 && n < m, then n = A1

On the code:

<?PHP$data=Array(0, 0, 0, 0, 1, 0, 0, 0, 0, 0);classFilterball { Public $theSpecial;//Special Ball 1 = heavy,-1 = Light     Public $balanceTimes= 0; /** param integer & array $left left ball * param integer & array $right right ball * return left ball weight =-1 same weight = 0 right Ball weight =1*/     Public functionBalance$left,$right) {        $this->balancetimes++; if(Is_array($left)) {            $left=Array_sum($left); }        if(Is_array($right)) {            $right=Array_sum($right); }        if($left==$right) {            return0; } ElseIf($left>$right) {            return-1; } Else {            return1; }    }    functionExecute$data) {        $mod=intval(Count($data)/3);//Modulo //divide the array into 3-4 arrays of each $mod element        $i= 0; $j= 0; $newArray=Array();  while($i<Count($data)) {            if(isset($newArray[$j]) &&Count($newArray[$j]) >=$mod) {                $j++; }            $newArray[$j][] =$data[$i]; $i++; }        $return 0=$this->balance ($newArray[0],$newArray[1]); $return 1=$this->balance ($newArray[0],$newArray[2]); Switch((string)$return 0. (string)$return 1) {             Case' 11 ':$this->thespecial =-1; return0 +$this->comparethird ($newArray[0]);  Case' 10 ':$this->thespecial = 1; return3 +$this->comparethird ($newArray[1]);  Case' 01 ':$this->thespecial = 1; return6 +$this->comparethird ($newArray[2]);  Case' 00 ':return9;  Case' 0-1 ':$this->thespecial =-1; return6 +$this->comparethird ($newArray[2]);  Case'-10 ':$this->thespecial =-1; return3 +$this->comparethird ($newArray[1]);  Case'-1-1 ':$this->thespecial = 1; return0 +$this->comparethird ($newArray[0]); }    }    /** Through the first two pairs, know whether the special ball is heavy or light, and then in three balls in a pair, know three balls in the index of the special ball*/     Public functionComparethird ($childArray) {        $return=$this->balance ($childArray[0],$childArray[1]); Switch($return) {             Case1: Case-1:if($return==$this-thespecial) {                    return1; } Else {                    return0; }                 Break;  Case0:return2;  Break; }    }}Echo' <pre> ';Print_r($data);$filter=NewFilterball ();Echo sprintf(' Key value:%s,%s times ',$filter->execute ($data),$filter->balancetimes);

10 small balls with a balance to find the one with different weights.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.