Array. prototype. pointer = 0; // simulate the internal pointer of the Array. // Reset function, which returns the pointer inside the Array (pointing to the first element) Var reset = function (arrayObj ){ If (! (ArrayObj instanceof Array )){ Alert ("Reset () function parameter type error! Check the input! "); Return; } ArrayObj. pointer = 0; } // Current function, returns the Current element pointed to by the internal pointer of the array Var current = function (arrayObj ){ If (! (ArrayObj instanceof Array )){ Alert ("Current () function parameter type error! Check the input! "); Return; } Return arrayObj [arrayObj. pointer]; } // End function, pointing the internal pointer of the array to the last element Var end = function (arrayObj ){ If (! (ArrayObj instanceof Array )){ The alert ("End () function parameter type is incorrect! Check the input! "); Return; } ArrayObj. pointer = arrayObj. length-1; Return arrayObj [arrayObj. pointer]; } // Next function, which moves the internal pointer of the array one by one // If it has already pointed to the last element, FALSE is returned. Var next = function (arrayObj ){ If (! (ArrayObj instanceof Array )){ The alert ("Next () function parameter type is incorrect! Check the input! "); Return; } ArrayObj. pointer ++; If (typeof arrayObj [arrayObj. pointer] = 'undefined '){ ArrayObj. pointer --; Return false; } Return true; } // Prev function, which moves the internal pointer of the array one bit // If it has already pointed to the first element, FALSE is returned. Var prev = function (arrayObj ){ If (! (ArrayObj instanceof Array )){ The alert ("Prev () function parameter type is incorrect! Check the input! "); Return; } ArrayObj. pointer --; If (typeof arrayObj [arrayObj. pointer] = 'undefined '){ ArrayObj. pointer ++; Return false; } Return arrayObj [arrayObj. pointer]; } // Unset function, delete the specified array element Var unset = function (index, arrayObj ){ If (! (ArrayObj instanceof Array )){ The alert ("Unset () function parameter type is incorrect! Check the input! "); Return; } If (typeof arrayObj [index] = 'undefined '){ Alert ("Unset () function parameter index error! This element does not exist! "); Return false; } ArrayObj. splice (index, 1 ); Return true; } // Search function, returns the key name of the array through the array key value Var search = function (value, arrayObj ){ If (! (ArrayObj instanceof Array )){ The alert ("Search () function parameter type is incorrect! Check the input! "); Return; } For (index in arrayObj ){ If (arrayObj [index] = value ){ Return index; } } Return false; } // GetKingMonkey function, which is the main function of Joseph. n monkeys are counted to m. Function getKingMonkey (n, m ){ A = new Array (); For (I = 1; I <= n; I ++ ){ A [I] = I; } A [0] = 0; unset (0, a); reset (); While (a. length> 1 ){ For (counter = 1; counter <= m; counter ++ ){ If (next ()){ If (counter = m ){ Unset (search (prev (a), a), ); } } Else { Reset (); If (counter = m ){ Unset (search (end (a), a), ); Reset (); } } } } Return current (); } Alert ("Monkey King number:" + getKingMonkey (100, 17 )); |