9个人选出一个领导,将剩下所有人排成一个圆周,沿这个圆周每次数5个人就排除出队者,有人出列后,剩下的人靠拢,仍然保持一个完整的圆周。找出最后剩下的那个人是谁?(水平有限,写的不规范)
#include <iostream>
#include<string>
using namespace std;
typedef struct node* link;
struct node
{
int item;
link next;
};
int main()
{
int i=0,M=5,N=9;
node *x,*t;
x=t=new node;
x->item =1;
for(i=2;i<=N;i++)
{
t->next=new node;
t=t->next;
t->item=i;
}
t->next=x;
while(t!=t->next)
{
for(i=1;i<M;i++)
t=t->next;
link p = t->next;
t->next=t->next->next;
delete(p);
N-- ;
}
cout<<t->item<<endl;
int temp;
cin>>temp;
return 0;
}