I have provided answers to the first three questions about the front-end, and I will give answers to the fourth question for your reference and study. If the error is correct, please correct it. Thank you.
Bils [] defines an array with a length of 12
Status indicates the weight of the ball.-1 indicates that light 0 indicates normal. 1 indicates heavy.
Randomly generate a state of-1 or 1, and place it in the bils array randomly, and record the position of the Bad ball in the array.
Below is a simple simulation example.
Simple simulated test:
If mstatustype =-1, the bad ball is light
Mballpos = 3; the position of the Bad ball is placed in the fourth
Leftbils = [,]; record position, which is the balls 1, 2, 3, and 4 respectively.
Rightbils = [,]; record positions, which are 5, 6, 7, and 8 balls.
Call resultmsg (leftbils, rightbils );
VaR bils = new array (); // store the ball var status = [-]; //-1 indicates light 1 indicates heavy var mstatusnum = parseint (math. random () * 2); // randomly generate 0 or 1 var mballpos = parseint (math. random () * 12); // The Position of the Bad ball var mstatustype = status [mstatusnum]; // The Weight Information var leftbils = new array (); // call the left-side ball var rightbils = new array (); // call the right-side ball/*** function initbils to initialize this group of 12 balls */function initbils () {for (VAR I = 0; I <12; I ++) {if (I = mballpos) {bils [I] = mstatustype;} else {bils [I] = 0 ;}}/ *** the isball function checks whether the ball contains a bad ball and returns true, no reverse return false * parameter arr index group, index group, the ball contained on one side * parameter MPOs indicates the position of the Bad ball */function isball (ARR, MPOs) {for (VAR I = 0, j = arr. length; I <j; I ++) {If (ARR [I] === MPOs) {return true ;}} return false ;} /*** the resultmsg function displays the result and returns the string type reminder weight information * parameter leftbils index group, which indicates the left-side ball in the middle. * parameter rightbils index group, indicates the right ball */function resultmsg (leftbils, rightbils) {If (leftbils. length> rightball S. length) {// The console is heavy when the ball is over there. log ("weight on the left side");} else if (leftbils. length <rightbils. length) {console. log ("Left is lighter than right");} else {// There is a bad ball (based on the position), and Judge or light or heavy if (based on-1 or 1 (! Isball (leftbils, mballpos )&&! Isball (rightbils, mballpos) {console. log ("same weight on both sides");} else if (isball (leftbils, mballpos) {If (mstatustype ===-1) {console. log ("Left is lighter than right")} else {console. log ("weight on the left side") ;}} else if (isball (rightbils, mballpos) {If (mstatustype ===- 1) {console. log ("weight on the left")} else {console. log ("the left side is lighter than the right side ");}}}}
For more information, see
There are 12 small balls with a different quality from the other eleven. I don't know whether it is heavy or light. Weigh the ball of different quality three times with a single scale (Source: 12 ball weighing problem)
Set the number of 12 balls to 1 ~ C
1 Comparison 1234 & 5678
1.1 1234 = 5678, then the bad ball is in 9abc, the second name is 1239 & 56ab
1.1.1 1239 = 56ab, the bad ball is C
1.1.2 1239> 56ab, the bad ball is in 9ab, and 9> AB is called A & B for the third time
1.1.2.1 A = B, 9 for bad balls, 9 for heavy
1.1.2.2 A> B. The Bad ball is B, and B is light.
1.1.2.3 A <B, Bad ball is a, and a is light
1.1.3 1239 <56ab, the bad ball is in 9ab, and 9 <AB is called A & B for the third time
1.1.3.1 a = B, Bad ball is 9, 9 Light
1.1.3.2 A> B. The Bad ball is a, and a is too heavy.
1.1.3.3 A <B, Bad ball is B, B is heavy
1.2 1234> 5678, the bad ball is 1 ~ Medium 8, and either 1234 has a heavy-weight bad ball, or 5678 has a light-weight bad ball, said 2345 & ABC1
1.2.1 2345 = ABC1, the bad ball is in 678, the third name is 6 & 7
1.2.1.1 6 = 7, the bad ball is 8 to 8 light
1.2.1.2 6> 7, the bad ball is 7, and the 7 is light
1.2.1.3 6 <7, the bad ball is 6, 6 is light
1.2.2 2345> ABC1, the bad ball is in 234, because if 234 is normal, it means 5> 1. Obviously 1.2 1234> 5678 is not true, and the third name is 2 & 3.
1.2.2.1 2 = 3, 4 is a bad ball, 4 is heavy
1.2.2.2 2> 3, 2 is a bad ball, 2 is heavy
1.2.2.3 2 <3, 3 is a bad ball, 3 is heavy
1.2.3 2345 <ABC1, indicating that the bad ball is in 51, because if 51 is normal, it means 234 <ABC, apparently 1.2 1234> 5678 is not true, restraint 5 <1, the third time said 1 &
1.2.3.1 1 = A, Bad ball is 5, 5 light
1.2.3.2 1> A. The Bad ball is heavy.
1.2.3.3 1 <A, this situation does not exist
1.3 1234 <5678, with the same judgment method as 1.2