#include <stdio.h>
#include <stdlib.h>
struct yyh
{
int data;
struct yyh *next;
};
int main()
{
struct yyh *p,*head,*tail,*q;
head=malloc(sizeof(struct yyh));
head->next=NULL;
head->data=1;
tail=head;
int n,m;
scanf("%d %d",&n,&m);
for(int i=2;i<=n;i++){
p=malloc(sizeof(struct yyh));
p->next=NULL;
p->data=i;
tail->next=p;
tail=p;
}
tail->next=head;
q=malloc(sizeof(struct yyh));
q=tail;
int num=0;
int count=0;
while(count<n-1)
{
p=q->next;
num++;
if(num%m==0)
{
q->next=p->next;
free(p);
count++;
}
else
q=p;
}
printf("%d",q->data);
return 0;
}
SDUT 1197 约瑟夫问题
最新推荐文章于 2021-04-14 17:42:51 发布