Example of Joseph's problem using pointer operations in JavaScript

Source: Internet
Author: User

Example of Joseph's problem using pointer operations in JavaScript

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.

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

43

44

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

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

Array. prototype. pointer = 0; // simulate the internal pointer of the Array.

// Reset function, which returns the pointer inside 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 pointed to by the internal pointer of the array

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 )){

The alert ("End () function parameter type is incorrect! Check the input! ");

Return;

}

ArrayObj. pointer = arrayObj. length-1;

Return arrayObj [arrayObj. pointer];

}

// Next function, which moves the internal pointer of the array one by one

// If it has already pointed to the last element, FALSE is returned.

Var next = function (arrayObj ){

If (! (ArrayObj instanceof Array )){

The alert ("Next () function parameter type is incorrect! Check the input! ");

Return;

}

ArrayObj. pointer ++;

If (typeof arrayObj [arrayObj. pointer] = 'undefined '){

ArrayObj. pointer --;

Return false;

}

Return true;

}

// Prev function, which moves the internal pointer of the array one bit

// If it has already pointed to the first element, FALSE is returned.

Var prev = function (arrayObj ){

If (! (ArrayObj instanceof Array )){

The alert ("Prev () function parameter type is incorrect! 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 )){

The alert ("Unset () function parameter type is incorrect! 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, returns the key name of the array through the array key value

Var search = function (value, arrayObj ){

If (! (ArrayObj instanceof Array )){

The alert ("Search () function parameter type is incorrect! Check the input! ");

Return;

}

For (index in arrayObj ){

If (arrayObj [index] = value ){

Return index;

}

}

Return false;

}

// GetKingMonkey function, which is the main function of Joseph. n monkeys are counted 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 ();

While (a. length> 1 ){

For (counter = 1; counter <= m; counter ++ ){

If (next ()){

If (counter = m ){

Unset (search (prev (a), a), );

}

} Else {

Reset ();

If (counter = m ){

Unset (search (end (a), a), );

Reset ();

}

}

}

}

Return current ();

}

Alert ("Monkey King number:" + getKingMonkey (100, 17 ));

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.