the solution of Joseph (Josephus) problem--Using cyclic list
1. The reference to Joseph's question
The question of Joseph (Joseph Ring) is a mathematical application problem.
known n individuals (numbered 1,2,3...N, respectively) sitting around a round table, from the number of people numbered K, count to M the man out, his next person from 1 start off, Count to M of the person again out, according to this rule repeat, until the round table all the people around.
usually when solving this kind of problem, we put the number from 1~n, and the final result number is the solution of the original problem.
2. The algorithm principle of solving Joseph problem
Note: n=8,m=3 Joseph problem example, if n=8,k=1,m=3, then the order of the column will be 3,6,1,5,2,8,4, initially numbered 7 is the solution of Joseph Problem.
(1) A group of people surrounded by a ring, a total of 8 people, n=8.
(2) numbered from a number, assuming that from the first person to start off, k=1.
(3) count to a certain number of times, the person out of the next person, set off the number of 3,m=3 for the column.
(4) loop until all the men are out, and Joseph's ring is over.
the algorithm principle of solving Joseph problem diagram:
3. Solving Joseph problem with cyclic chain list
the definition of node structure of 3.1 linked list
file: LinkNode.h
#ifndef link_node_h_
#define Link_node_h_
#include <iostream>
#include <string>
# Include <strstream>
using namespace std;
Template <class t>
struct linknode //Link List node class definition
{
T data; Data domain
linknode<t> *link; Pointer field--subsequent pointer
//only constructor that initializes pointer members
Linknode (linknode<t>* ptr = NULL) {link = ptr;}
Initializes the constructor of the data with the pointer member
linknode (const t& value, linknode<t>* ptr = NULL) {data = value; link = ptr;}
};
#endif/* link_node_h_ * *
class definition of 3.2 cyclic chain list and implementation of its operation (with additional header node)
file: CircLinkedList.h
#ifndef circ_linked_list_h_ #define Circ_linked_list_h_ #include "LinkNode.h" template <class t> class Circl inkedlist//with additional head node {public:circlinkedlist (); constructor function ~circlinkedlist (); destructor public:linknode<t>* Locate (int i) const; Gets the first node and returns the bool Insert (int i, const t& x); Inserting a new node linknode<t>* Remove (linknode<t>* prenode, t& x) with a data value of x after the first node; Delete the node and save the data value of the deleted node to X, and finally return the next node void Makeempty () of the deleted node; Clear the list public:linknode<t>* gethead () const; Returns the address of the additional header node private:linknode<t> *first;
The head node of the linked list}; Constructor Template<class t> circlinkedlist<t>::circlinkedlist (): A (new linknode<t>) {First->li
NK = A;
cout << "$ execute constructor" << Endl; }//destructor Template<class t> circlinkedlist<t>::~circlinkedlist () {cout << "$ perform destructor" << Endl;
Makeempty ();
//Get the first node and return Template<class t> linknode<t>* circlinkedlist<t>::locate (int i) const {if (I < 0)
{return NULL; if ((First->link) | |
(0 = i))
{return A;
int location = 1;
linknode<t> *curnode = first->link;
while ((Location < i) && (!= Curnode)) {Curnode = curnode->link;
location++;
} if (A/= Curnode) {curnode = NULL;
return curnode; Template<class t> bool Circlinkedlist<t>::insert (int i, const t& x) {Linknode After the first node inserts a new node with a data value of X
<T> *curnode = Locate (i);
if (NULL = = Curnode) {return false;
} linknode<t> *newnode = new linknode<t> (x);
if (NULL = = NewNode) {cerr << "* Storage allocation Error" << Endl;
Exit (1);
} Newnode->link = curnode->link; Curnode->link = newNode;
return true; //delete the node and save the data value of the deleted node to X, and finally return the next node of the deleted node Template<class t> linknode<t>* Circlinkedlist<t>::remove (
linknode<t>* Prenode, t& x) {if (null = = Prenode) {return null;
} linknode<t> *delnode = prenode->link;
if (A/= Delnode) {Delnode = first->link;
First->link = delnode->link;
else {Prenode->link = delnode->link;
} x = delnode->data;
linknode<t> *nextnode = delnode->link;
Delete Delnode;
return nextnode; }//Purge list (keep additional header node) Template<class t> void Circlinkedlist<t>::makeempty () {linknode<t> *curnode = NU
LL; while (the!= first->link)//When the list is not empty, delete all nodes in the list {Curnode = first->link; Save deleted node First->link = curnode->link; Pointing to the next node of the deleted node, delete curnode, the pointer field in the appended head node of the linked list; Remove the deleted node from the linked list}//Return the address of the additional header node TemplAte<class t> linknode<t>* circlinkedlist<t>::gethead () const {return a
#endif/* Circ_linked_list_h_ * *
3.3 Algorithm realization of Joseph Ring solution
file: Josephus.h
#ifndef josephus_h_ #define Josephus_h_ #include "CircLinkedList.h"//With additional head nodes template <class t>
T Josephus (circlinkedlist<t>* Js, const int n, const int k, const int m) {T data;
linknode<t> *prenode = NULL;
linknode<t> *curnode = js->locate (k); for (int i = 0; i < n-1. i++) {for (int j = 1; j < m; j) {if (
Js->gethead () = = Curnode) {Curnode = curnode->link;
} Prenode = Curnode;
Curnode = curnode->link;
} Curnode = Js->remove (prenode, data);
cout << "out-" << data << Endl;
} linknode<t> *solutionnode = Js->gethead ()->link;
Return solutionnode->data; #endif/* Josephus_h_ * * * **3.4 main function (main function) Implementation * * * file: main.cpp** ' Ruby: #Include "Josephus.h"//with additional header//to determine whether each character of the input string is numeric 1~9 bool Isnumber (const string& s_num) {for (size_t i = 0; I < s_num.size (); i++) {if (S_num[i] < ' 1 ') | |
(S_num[i] > ' 9 ")
{return false;
} return true;
}//input parameter int get_parameter (const string& parameter_name) {cout << parameter_name;
String S_item;
Cin >> S_item;
while (false = = Isnumber (S_item)) {cout << "* Input is incorrect, please re-enter:";
Cin >> S_item;
Return Atoi (S_item.c_str ()); }//construct Joseph Ring template <class t> circlinkedlist<t>* Construct_josephus () {cout << "==> create Joseph Ring"
;< Endl;
circlinkedlist<t> *cirlinkedlist = new circlinkedlist<t>;
return cirlinkedlist; }//destructor Joseph ring template <class t> void Destory_josephus (circlinkedlist<t>* cirlinkedlist) {cout << "=
> Release Joseph Ring in the heap to apply for the space, and will point to the space pointer variable null "<< Endl;" Delete cirlinkedList;
Cirlinkedlist = NULL;
int main (int argc, char* argv[]) {int n = get_parameter ("> Please enter total number, n =");
int k = Get_parameter ("> Please enter number of start count, k =");
int m = get_parameter ("> Please enter a count interval, M =");
circlinkedlist<int> *cirlinkedlist = construct_josephus<int> ();
for (int i = 0; i < n; i++) {Cirlinkedlist->insert (I, i+1);
int left = josephus<int> (Cirlinkedlist, N, K, m);
cout << "Joseph Ring solution, Solution =" << left << Endl;
Destory_josephus (cirlinkedlist);
System ("pause");
return 0; }
References:
[1] "Data structures (described in object-oriented methods and C + + language) (2nd edition)" Yin-chapter II
[2] "Common algorithm Manual for C/A + +" Qin Yuhua, Xiangxuyu--eighth chapter
[3] Baidu Search Keywords: joseph question, Joseph Ring