#include<stdio.h>
struct node
{
int data;
struct node *next;
};
int main()
{
int n,m,k,i,j=0;
struct node *head,*p,*q,*tail;
scanf("%d%d",&n,&m);
head=(struct node*)malloc(sizeof(struct node));
head->data=1;
head->next=NULL;
tail=head;
for(i=2;i<=n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->data=i;
tail->next=p;
tail=p;
}
k=0;
j=0;
tail->next=head;
for(p=head; ;p=p->next)
{
k++;
if(k+1==m&&p->next!=p)
{
q=p->next;
p->next=p->next->next;
free(q);
k=0;
}
if(p->next==p)
{
printf("%d",p->data);
break;
}
}
}
C语言|链表-约瑟夫问题
于 2022-03-22 17:31:19 首次发布