JavaScript uses pointer manipulation to implement the _javascript technique of a Josephson problem instance

Source: Internet
Author: User
Tags prev

This example describes the way JavaScript uses pointer manipulation to implement the Josephson problem. Share to everyone for your reference. The specific analysis is as follows:

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

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, move the inner pointer of an array up one bit//////If you have pointed to the first element, return FALSE var Prev = function (arrayobj) {if (!) ( Arrayobj instanceof Array)) {alert ("Prev () function argument type is wrong!) 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 (Search (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));

The

wants this article to help you with your JavaScript programming.

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.