Data structure summary series (three) - Joseph circular list of issues

Joseph Problem description:

Joseph problem is a famous problem: N personal circle, the number of starting from the first report, the first of M will be killed, the last remaining one, the rest will be killed. For example, N = 6, M = 5, the order being killed is: 5,4,6,2,3,1.
analysis:
(1) for each person since the dead and alive only two states, so each can be labeled with a state array Brown, available true that the dead, live indicates to false.
(2) at the start of each person is alive, so the array initial value assigned to all false.
(3) simulated killing process until all people have been killed so far.
(Because cancer lazy bloggers made this way will lead to it ~ ~ ~ just after the interception of the contents of a degree Wikipedia, and perhaps update)

We now determine the use of the circular list, then you start to define:

Since the circular list tail pointer points to the head, it defines circular linked list and single linked list consistent, showing only slight differences.

show code:

typedef struct Node 
{ 
    int Data;
     struct Node * Next; // to point to the next node 
} LinkNode; 

typedef LinkNode * RollList; // redefine the circular linked list head node

insert:

void Creat (R & lt RollList &, int length) 
{ 
    RollList tmp = R & lt; // here modified tmp, to avoid direct node modifications head 
    for ( int I = 2 ; I <= length; I ++) // because each node is determined data, the first node out separately defined 
    { 
        LinkNode * L = new new LinkNode; 
        L -> data = I; 
        L -> Next = R & lt; 
        tmp -> Next = L; // here is simple insertion 
        tmp tmp- => Next; 
    } 
}

Kill function:

void Kill (R & lt RollList &, int m, int length) 
{ 
    RollList tmp = R & lt;
     for ( int I = . 1 ; I <= Length- . 1 ; I ++) // loop n-1 times. 
    {
         For ( int J = . 1 ; J <= M- 2 ; J ++) // First came here to be deleted before the node is a node 
        { 
            tmp = tmp-> Next; 
        } 
        COUT << " Kill The " << tmp- > next-> the Data << " people "<< endl; 
        tmp -> Next = tmp-> next-> Next; // here modify pointer (skip the deleted node) 
        tmp = tmp-> Next; 
    } 
    COUT << endl; 
    COUT << " Survive people: " << endl; 
    COUT << tmp-> Data << endl; 
}

Well, we already solved the problem of Joseph:

Directly on the code:

#include <bits / STDC ++ H.> the using namespace STD; int ARR [ 200 is ]; 
typedef struct Node 
{ int Data;
     struct Node * Next; // to point to the next node } LinkNode; 
typedef LinkNode * RollList; // redefined circular list head node void Creat (RollList & R & lt, int length) 
{ 
    RollList tmp = R & lt; // here modified tmp, to avoid direct head node modified for ( int I = 2 ; I <= length; I ++) // Since determining data for each node, the first node out separately defined 
        LinkNode

 



    




    
     {= L * new new LinkNode; 
        L -> Data = I; 
        L -> Next = R & lt; 
        tmp -> Next = L; // inserted here also very simple 
        tmp = tmp-> Next; 
    } 
} 


// note below representatives another method of displaying human survival (fast discharge is really leather) 
/ * void Kill (R & lt RollList &, int m, int length, int ARR []) 
{ 
    RollList tmp = R & lt; 
    for (int I =. 1; i <= length-1; i ++) // loop n-1 times. 
    { 
        For (int. 1 = J; J <= m-2; J ++) 
        { 
            tmp = tmp-> Next; 
        } 
        tmp-> Next = tmp-> next-> Next; 
        tmp = tmp-> Next; 
        COUT << "The Kill" << tmp-> next-> << Data "people" <
        ARR [I] = tmp-> next-> Data; 
    } 
} * / 

void Kill (R & lt RollList &, int m, int length) 
{ 
    RollList tmp = R & lt;
     for ( int I = . 1 ; I <= Length- . 1 ; I ++ ) // cycle n-1 times. 
    {
         For ( int J = . 1 ; J <= M- 2 ; J ++) // First came here to be deleted before the node is a node 
        { 
            tmp = tmp-> Next; 
        } 
        COUT << " Kill The "Tmp- <<> next-> << Data " people " << endl; 
        tmp -> Next = tmp-> next-> Next; // here modify pointer (skip the deleted node) 
        tmp = tmp- > Next; 
    } 
    COUT << endl; 
    COUT << " Survive people: " << endl; 
    COUT << tmp-> Data << endl; 
} 

int main () 
{ 
    RollList R & lt = new new LinkNode; 
    R & lt -> Data = . 1 ; 
    R & lt ->next=R;
    int length, m;
    cost<< "input people :" << endl;
    cin >> length;
    Creat(R,length);
    cout << "input death :" << endl;
    cin >> m;
    cout << endl;
    /*Kill(R,m,length,arr);
    cout << "Survive people :" << endl;
    sort(arr,arr+length);
    for(int i=1;i<=length-1;i++)
    {
        if(arr[i]!=i)
        {
            cout << i << endl;
            break;
        }
    }*/
    Kill(R,m,length);
    return 0;
}

I have to say (fast row leather !!!!)

Secretly talk about (and perhaps then there will be another way to solve the problem of Joseph, but lazy bloggers do not know when cancer will be updated, but fast holiday it ~ ~)

Guess you like

Origin www.cnblogs.com/ever17/p/10959304.html