JavaScript uses pointer operations to implement Joseph's problem instance, javascript Joseph
This article describes how to implement Joseph's problem using pointer operations in JavaScript. Share it with you for your reference. The specific analysis is as follows:
Before implementation, you must compile some operation functions for internal pointers of JS arrays, such as reset (), current (), next (), prev (), search (), end () these functions are all implemented by ourselves, because JS does not have these magic operation functions built in.
Array. prototype. pointer = 0; // simulate the internal pointer of the array // Reset function to normalize the internal pointer of 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 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) {alert ("End () function parameter type error! Check the input! "); Return;} arrayObj. pointer = arrayObj. length-1; return arrayObj [arrayObj. pointer];} // Next function, which moves the pointer inside the array one by one // if it has already pointed to the last element, return FALSEvar next = function (arrayObj) {if (! (ArrayObj instanceof Array) {alert ("Next () function parameter type error! Check the input! "); Return;} arrayObj. pointer ++; if (typeof arrayObj [arrayObj. pointer] = 'undefined') {arrayObj. pointer --; return false;} return true;} // Prev function, move the internal pointer of the array one by one // if it has already pointed to the first element, FALSEvar prev = function (arrayObj) {if (! (ArrayObj instanceof Array) {alert ("Prev () function parameter type error! 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) {alert ("Unset () function parameter type error! 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, return the array key name var search = function (value, arrayObj) {if (! (ArrayObj instanceof Array) {alert ("Search () function parameter type error! Check the input! "); Return ;}for (index in arrayObj) {if (arrayObj [index] == value) {return index ;}} return false ;}// getKingMonkey function, our Joseph main function, n monkeys, counts to mfunction getKingMonkey (n, m) {a = new Array (); for (I = 1; I <= n; I ++) {a [I] = I;} a [0] = 0; unset (0, a); reset (a); while (. length> 1) {for (counter = 1; counter <= m; counter ++) {if (next (a) {if (counter = m) {unset (search (prev (a), a), a) ;}} else {reset (a); if (counter = m) {unset (search (end (a), a), a); reset (a) ;}}} return current ();} alert ("Monkey King number:" + getKingMonkey (100, 17 ));
I hope this article will help you design javascript programs.