/*
Josephus问题 -- n个人围成一圈,按顺序数数,每次第m个人出局,求最后一个
Wirtten by: nick
Date: 2012-10-18 19:56
*/
#include <iostream>
#include <iomanip>
using namespace std;
struct node
{
int item;
node *next;
node(int x, node* t)
{
item = x;
next = t;
}
};
typedef node *link;
int main()
{
int n,m;
n=9;
m=5;
link t = new node(1, 0);
t->next = t;
link x = t;
for(int i=2; i<=n; i++)
x = (x->next = new node(i, t));
link tmp;
while(x != x->next)
{
for(int i=1; i<m; i++)
x = x->next;
cout << setw(5) << x->next->item; //输出每次淘汰的结点
tmp = x->next;
x->next = x->next->next;
delete tmp;
}
cout << endl << x->item ; //只剩一个节点
return 0;
}