#include<stdio.h>
#include<stdlib.h>
typedef struct list
{
int num;
int living;
struct list*next;
}List;
List *set(int n);
int main()
{
List *print;
int n,m,out=0,cnt=0;
scanf("%d %d",&n,&m);//n为人数,m为第m个淘汰的人
print=set(n);
while(out!=n)
{
if(print->living)
cnt++;
if(cnt==m)
{
printf("%d ",print->num);
cnt=0;
out++;
print->living=0;
}
print=print->next;
}
}
List* set(int n)
{
int i;
List* head, * p;
head=(List*)malloc(sizeof(List));
p=head;
for(i=1;i<=n;i++)
{
p->next = (List*)malloc(sizeof(List));
p = p->next;
p->num=i;
p->living=1;
}
head = head->next;
p->next = head;
return head;
}
使用循环链表实现约瑟夫斯置换
最新推荐文章于 2021-11-03 23:09:15 发布