1.数组实现形式
#include<iostream>
#define LEN 40
#define Interval 3
int main()
{
int R[LEN];
for(int i=0;i<LEN;i++)
R[i]=1;//数组初始化 1:活着 0:自杀
int lefcount=LEN;//剩下的人数
int index=0,count=0;;//数组下标 统计计数
while(leftcount>2)
{
if(R[index]==1)
{
count++;
if(count==Interval)
{
R[index]=0;
count=0;
leftcount--;
}
index++;
if(index==LEN)
index=0;
}
}
for(int j=0;j<LEN;j++)
if(R[j]==1)
cout<<j<<" ";
return 0;
}
2.循环链表
typedef struct LNode{
int data;
struct LNode *next;
}LNode;
void create(LNode *&L)
{
LNode *r,*s;
L=(LNode*)malloc(sizeof(LNode));
r=L;
for(i=1;i<=n;i++)
{
s=(LNode*)malloc(sizeof(LNode));
s->data=i;
r->next=s;
r=s;
}
r->next=L->next;
}
void yuesehuan(LNode *L,int m,int n)
{
LNode *p=L->next;
LNode *temp;
for(int i=1;i<m/2;i++)//淘汰一半人数
{
for(int j=0;j<n-1;j++)
p=p->next;
temp=p->next;
p->next=temp->next;p=temp->next;
free(temp);
}
}