Home > Web Front-end > JS Tutorial > JavaScript uses pointer operations to implement Joseph's problem examples_javascript skills

JavaScript uses pointer operations to implement Joseph's problem examples_javascript skills

WBOY
Release: 2016-05-16 16:05:17
Original
899 people have browsed it

The example in this article describes how JavaScript uses pointer operations to implement the Joseph problem. Share it with everyone for your reference. The specific analysis is as follows:

Of course, before implementation, you must write some operation functions for the internal pointers of JS arrays, such as: reset(), current(), next(), prev(), search(), end(). We all have these functions. You have to implement it yourself, because JS does not have these magical operation functions built in

Array.prototype.pointer = 0;//模拟数组内部指针
//Reset 函数,将数组内部指针归位(指向第一个元素)
var reset = function(arrayObj){
  if(!(arrayObj instanceof Array)){
    alert("Reset() 函数参数类型错误!请检查输入!");
    return;
  }
  arrayObj.pointer = 0;
}
//Current 函数,返回数组内部指针指向的当前元素
var current = function(arrayObj){
  if(!(arrayObj instanceof Array)){
    alert("Current() 函数参数类型错误!请检查输入!");
    return;
  }
  return arrayObj[arrayObj.pointer];
}
//End 函数,将数组内部指针指向最后一个元素
var end = function(arrayObj){
  if(!(arrayObj instanceof Array)){
    alert("End() 函数参数类型错误!请检查输入!");
    return;
  }
  arrayObj.pointer = arrayObj.length - 1;
  return arrayObj[arrayObj.pointer];
}
//Next 函数,将数组内部指针下移一位
//如果已经指向最后一个元素则返回 FALSE
var next = function(arrayObj){
  if(!(arrayObj instanceof Array)){
    alert("Next() 函数参数类型错误!请检查输入!");
    return;
  }
  arrayObj.pointer ++;
  if(typeof arrayObj[arrayObj.pointer] == 'undefined'){
    arrayObj.pointer --;
    return false;
  }
  return true;
}
//Prev 函数,将数组内部指针上移一位
//如果已经指向第一个元素则返回 FALSE
var prev = function(arrayObj){
  if(!(arrayObj instanceof Array)){
    alert("Prev() 函数参数类型错误!请检查输入!");
    return;
  }
  arrayObj.pointer --;
  if(typeof arrayObj[arrayObj.pointer] == 'undefined'){
    arrayObj.pointer ++;
    return false;
  }
  return arrayObj[arrayObj.pointer];
}
//Unset 函数,删除指定的数组元素
var unset = function(index, arrayObj){
  if(!(arrayObj instanceof Array)){
    alert("Unset() 函数参数类型错误!请检查输入!");
    return;
  }
  if(typeof arrayObj[index] == 'undefined'){
    alert("Unset() 函数参数 index 错误!不存在此元素!");
    return false;
  }
  arrayObj.splice(index, 1);
  return true;
}
//Search 函数,通过数组键值返回数组的键名
var search = function(value, arrayObj){
  if(!(arrayObj instanceof Array)){
    alert("Search() 函数参数类型错误!请检查输入!");
    return;
  }
  for(index in arrayObj){
    if(arrayObj[index] == value){
      return index;
    }
  }
  return false;
}
//getKingMonkey 函数,我们的约瑟夫主函数,n 只猴子,数到 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("猴子大王的编号为:" + getKingMonkey(100, 17));
Copy after login

I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template