实现代码:
#include <iostream>
#include <unistd.h>
using namespace std;
struct Node
{
int value;
struct Node *next;
};
class Joseph
{
public:
Joseph(int n):num(n)
{
head = new(struct Node);
head->next = NULL;
insert();
kill();
}
~Joseph(){delete head;}
void print();
void insert();
void kill();
private:
struct Node *head;
int num;
};
void Joseph::print()
{
if(NULL == head->next)
{
return;
}
struct Node *temp = new(struct Node);
temp->next= head->next;
while(temp->next->next != head->next)
{
cout << temp->next->value << " ";
temp->next = temp->next->next;
}
cout << temp->next->value << endl;
}
void Joseph::insert()
{
for(int i = 1; i <= num; i++)
{
struct Node *node = new(struct Node);
node->value = i;
if(NULL == head->next)
{
head->next = node;
node->next = node;
}
else
{
struct Node *temp = new(struct Node);
temp->next = head->next;
node->next = head->next;
while(temp->next->next != node->next)
{
temp->next = temp->next->next;
}
temp->next->next = node;
}
}
}
void Joseph::kill()
{
struct Node *temp = new(struct Node);
temp->next = head->next;
struct Node *kill = new(struct Node);
while(num-- > 1)
{
temp->next = temp->next->next;
kill->next = temp->next->next;
if(kill->next == head->next)
{
head->next = kill->next->next;
}
temp->next->next = kill->next->next;
temp->next = kill->next->next;
cout << "kill Num:" << kill->next->value << " ,\t剩余:" ;
print();
sleep(1);
}
}
int main()
{
Joseph jj(30);
}
运行结果:
kill Num:3 , 剩余:1 2 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
kill Num:6 , 剩余:1 2 4 5 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
kill Num:9 , 剩余:1 2 4 5 7 8 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
kill Num:12 , 剩余:1 2 4 5 7 8 10 11 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
kill Num:15 , 剩余:1 2 4 5 7 8 10 11 13 14 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
kill Num:18 , 剩余:1 2 4 5 7 8 10 11 13 14 16 17 19 20 21 22 23 24 25 26 27 28 29 30
kill Num:21 , 剩余:1 2 4 5 7 8 10 11 13 14 16 17 19 20 22 23 24 25 26 27 28 29 30
kill Num:24 , 剩余:1 2 4 5 7 8 10 11 13 14 16 17 19 20 22 23 25 26 27 28 29 30
kill Num:27 , 剩余:1 2 4 5 7 8 10 11 13 14 16 17 19 20 22 23 25 26 28 29 30
kill Num:30 , 剩余:1 2 4 5 7 8 10 11 13 14 16 17 19 20 22 23 25 26 28 29
kill Num:4 , 剩余:1 2 5 7 8 10 11 13 14 16 17 19 20 22 23 25 26 28 29
kill Num:8 , 剩余:1 2 5 7 10 11 13 14 16 17 19 20 22 23 25 26 28 29
kill Num:13 , 剩余:1 2 5 7 10 11 14 16 17 19 20 22 23 25 26 28 29
kill Num:17 , 剩余:1 2 5 7 10 11 14 16 19 20 22 23 25 26 28 29
kill Num:22 , 剩余:1 2 5 7 10 11 14 16 19 20 23 25 26 28 29
kill Num:26 , 剩余:1 2 5 7 10 11 14 16 19 20 23 25 28 29
kill Num:1 , 剩余:2 5 7 10 11 14 16 19 20 23 25 28 29
kill Num:7 , 剩余:2 5 10 11 14 16 19 20 23 25 28 29
kill Num:14 , 剩余:2 5 10 11 16 19 20 23 25 28 29
kill Num:20 , 剩余:2 5 10 11 16 19 23 25 28 29
kill Num:28 , 剩余:2 5 10 11 16 19 23 25 29
kill Num:5 , 剩余:2 10 11 16 19 23 25 29
kill Num:16 , 剩余:2 10 11 19 23 25 29
kill Num:25 , 剩余:2 10 11 19 23 29
kill Num:10 , 剩余:2 11 19 23 29
kill Num:23 , 剩余:2 11 19 29
kill Num:11 , 剩余:2 19 29
kill Num:2 , 剩余:19 29
kill Num:19 , 剩余:29