JavaScript USES pointer manipulation to implement the Joseph problem instance

  • 2020-05-27 04:14:12
  • OfStack

This article illustrates how JavaScript USES pointer manipulation to implement the Joseph problem. Share with you for your reference. The specific analysis is as follows:

Of course, we have to write some operation functions for the internal pointer of JS array, such as reset(), current(), next(), prev(), search(), end(). We have to implement these functions ourselves, because JS has no built-in magic operation functions


Array.prototype.pointer = 0;// Simulates an array internal pointer 
//Reset  Function to return the internal pointer to the array 1 An element) 
var reset = function(arrayObj){
  if(!(arrayObj instanceof Array)){
    alert("Reset()  Function parameter type error! Please check the input! ");
    return;
  }
  arrayObj.pointer = 0;
}
//Current  Function that returns the current element to which the pointer inside the array is pointing 
var current = function(arrayObj){
  if(!(arrayObj instanceof Array)){
    alert("Current()  Function parameter type error! Please check the input! ");
    return;
  }
  return arrayObj[arrayObj.pointer];
}
//End  Function to point the internal pointer to the end of the array 1 An element 
var end = function(arrayObj){
  if(!(arrayObj instanceof Array)){
    alert("End()  Function parameter type error! Please check the input! ");
    return;
  }
  arrayObj.pointer = arrayObj.length - 1;
  return arrayObj[arrayObj.pointer];
}
//Next  Function to move the pointer inside the array down 1 position 
// If it's pointing to the end 1 Element is returned  FALSE
var next = function(arrayObj){
  if(!(arrayObj instanceof Array)){
    alert("Next()  Function parameter type error! Please check the input! ");
    return;
  }
  arrayObj.pointer ++;
  if(typeof arrayObj[arrayObj.pointer] == 'undefined'){
    arrayObj.pointer --;
    return false;
  }
  return true;
}
//Prev  Function to move up the internal pointer of an array 1 position 
// If it's already pointing to the first 1 Element is returned  FALSE
var prev = function(arrayObj){
  if(!(arrayObj instanceof Array)){
    alert("Prev()  Function parameter type error! Please check the input! ");
    return;
  }
  arrayObj.pointer --;
  if(typeof arrayObj[arrayObj.pointer] == 'undefined'){
    arrayObj.pointer ++;
    return false;
  }
  return arrayObj[arrayObj.pointer];
}
//Unset  Deletes the specified array element 
var unset = function(index, arrayObj){
  if(!(arrayObj instanceof Array)){
    alert("Unset()  Function parameter type error! Please check the input! ");
    return;
  }
  if(typeof arrayObj[index] == 'undefined'){
    alert("Unset()  Function parameters  index  Error! This element does not exist! ");
    return false;
  }
  arrayObj.splice(index, 1);
  return true;
}
//Search  Function that returns the key name of an array by its key value 
var search = function(value, arrayObj){
  if(!(arrayObj instanceof Array)){
    alert("Search()  Function parameter type error! Please check the input! ");
    return;
  }
  for(index in arrayObj){
    if(arrayObj[index] == value){
      return index;
    }
  }
  return false;
}
//getKingMonkey  The delta function, our Joseph master function, n  One monkey, 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 current(a);
}
alert(" The number of Monkey King is: " + getKingMonkey(100, 17));

I hope this article is helpful for you to design javascript program.


Related articles: