JavaScript uses pointer manipulation to implement a Josephson problem instance

Source: Internet
Author: User
Tags prev

Before implementation of course to write some of the JS array of the internal pointer operation function, it's like: Reset (), current (), Next (), Prev (), search (), end () These functions, we all have to come to our own implementation, because JS does not have built-in these magical operation functions

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104-105 Array.prototype.pointer = 0;//analog array internal pointer//reset function, the array internal pointer (point to the first element) var Reset = function (arrayobj) {if (!) ( Arrayobj instanceof Array)} {alert ("Reset () function argument type is wrong!) Please check the input! "); Return } arrayobj.pointer = 0; The//current function, which returns the current element of the array's internal pointer to the var. = function (arrayobj) {if (! Arrayobj instanceof Array)} {alert ("current () function argument type is wrong!) Please check the input! "); Return return Arrayobj[arrayobj.pointer]; //end function, point the array internal pointer to the last element var end = function (arrayobj) {if (!) ( Arrayobj instanceof Array)} {alert ("End () function argument type is wrong!) Please check the input! "); Return } arrayobj.pointer = arrayobj.length-1; return Arrayobj[arrayobj.pointer]; //next function, move the inner pointer of an array down one bit////////////////////If the last element is already pointed, FALSE var Next = function (arrayobj) {if (!) Arrayobj instanceof Array)} {alert ("Next () function argument type is wrong!") Please check the input! "); Return } Arrayobj.pointer + +; if (typeof arrayobj[arrayobj.pointer] = = ' undefined ') {arrayobj.pointer-; return false;} return true; //prev function to move the inner pointer of an array//////////If you have pointed to the first element, return FALSE var Prev = function (arrayobj) {if (!) ( Arrayobj instanceof Array)) {alert ("Prev () function parameterNumber Type Error! Please check the input! "); Return } Arrayobj.pointer--; if (typeof arrayobj[arrayobj.pointer] = = ' undefined ') {arrayobj.pointer + +; return false;} return arrayobj[ Arrayobj.pointer]; The//unset function deletes the specified array element var Unset = function (index, arrayobj) {if (!) ( Arrayobj instanceof Array)) {alert ("Unset () function argument type is wrong!) Please check the input! "); Return } if (typeof arrayobj[index] = = ' undefined ') {alert ("Unset () function parameter index error!") This element is not present! "); return false; } arrayobj.splice (index, 1); return true; The//search function returns the key name of an array via array key value var Search = function (value, arrayobj) {if (!) ( Arrayobj instanceof Array)} {alert ("Search () function argument type is wrong!") Please check the input! "); Return For (index in arrayobj) {if (arrayobj[index] = = value) {return index}}, return false; //getkingmonkey function, our Joseph main function, n monkeys, count 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 (a); while (A.length > 1) {for (counter = 1; counter <= m counter + +) {if (next (a)) {if (counter = m) {unset (prev (a) , a), a); }else{Reset (a); if (Counter = = m) {unset (Search (End (a), a), a); reset (a);}} } Return to current (a); Alert ("Monkey King's number is:" + getkingmonkey (100, 17));
Related Article

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.