C++链表开发问题
PHP中文网
PHP中文网 2017-04-17 11:48:54
0
2
347
#include <iostream>
using namespace std;
struct child
{
    int num;
    child *link;
};
;void init(int n);
;void gameStart(int n,int m);
child *head;
child *present;
child *end;
;int main()
{
    int n,m;
    cout <<"请输入孩子的个数:";
    cin >>n;
    cout <<"请输入正整数m:";
    cin >>m;
    init(n);
    gameStart(n,m);
    cout <<"第" <<present->num <<"个孩子将获得胜利!" <<endl;
    delete present;
    return 0;
}
void init(int n)
{
    head=new child;
    head->num=1;
    present=head;
    for (int i=1;i<n;i++)
    {
        present->link=new child;
        present->link->num=i+1;
        present=present->link;
    }
    present->link=head;
    end=present;
    present=head;
}
void gameStart(int n,int m)
{
    child *pGuard=end;
    while (n!=1)
    {
        for (int j=1;j<m;j++)
        {
            pGuard=present;
            present=present->link;
        }
        pGuard->link=present->link;
        delete present;
        present=pGuard->link;
        n--;
    }
}

这是个约瑟夫环问题
我想问下

present->link=head;
end=present;
present=head;

具体什么意思
貌似是将首尾相连形成循环链表吧。。
请详细解释下。。谢谢! 求解。。

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(2)
小葫芦

@洃c小强 I saw this question in the middle of the night, so I’ll add something to it.


The problem with Joseph Ring is that there are 1..n prisoners numbered n sitting in a circle to report their numbers. The prisoner numbered m comes out of the queue and is killed, and then the next one of the prisoners who just came out of the queue is One person starts counting from 1, and so on, until there is only one person left. And report the person's number g(n,m).
This is a classic question in computer algorithms.
The conventional solution is to model based on the problem.
Among them

struct child{
    int num;
    child *link;
};

is the model of the prisoner. Each prisoner has a number num and a pointer to the next prisoner number link. In this way, a one-way linked list can be established.
However, the Joseph ring is cyclic from head to tail, so present->link=head; is needed to convert the one-way linked list into a ring linked list.
After completing the model construction, you need to specify the start and end positions

end=present;
present=head;

is used to complete this function.
The following gameStart is to complete the function of deleting the linked list node based on conditions.

Except for the background of the Joseph ring, in fact, there are only two basic functions of this program: 1. Build a ring linked list; 2. Delete ring linked list nodes according to conditions;
This makes the program much easier to understand. ^-^

In addition, the solution in the question uses simulation to solve the problem of Joseph ring. In fact, in mathematics, there is a direct answer to the Joseph Ring g(n,m)=?.

g(n,m) = (g(n – 1) + m) % n, (n > 1)

The code written in Python is as follows:

def josephus(n, m):
    j = 0
    for i in range(2, n + 1):
        j = (j + m) % i
    print j + 1

That’s probably it.

小葫芦

I didn’t look carefully and I don’t understand Joseph Ring and he doesn’t know C++. But it looks like it

present->link=head;
end=present;
present=head;

The purpose of these three sentences is to reset the link and end pointers of present and present so that they point to the head of the linked list. That is to say, the Joseph ring has been constructed at this time, and the pointers can be set to the initial value of the linked list. After using it later, I looked at the function name init. I think my guess was absolutely right

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template