哪个大哥给我发个哟瑟夫问题的解答,c语言版的

来源:百度知道 编辑:UC知道 时间:2024/06/20 05:08:45
搞不懂呀,最好多几种方法(如果有的话)

#include <conio.h>
#include <stdio.h>
#include <malloc.h>

typedef struct num
{
int data;
struct num *next;
}node;

int main()
{
node *p1,*p2,*head;
int i,m,n,count=1;
scanf("%d %d",&n,&m);

head=p1=(node*)malloc(sizeof(node));
for(i=0;i<n;i++)
{
p2=(node*)malloc(sizeof(node));
p2->data=i+1;
p1->next=p2;
p1=p2;
}
p1->next=head->next;
free(head);
p1=p1->next;
while(p1!=p1->next)
if(count==m)
{
p2=p1->next;
p1->data=p2->data;
p1->next=p2->next;
free(p2);
count=1;
}
else
{
p1=p1->next;
count++;
}
printf("%d",p1->data);

getch();
return 0;
}

# include <stdio.h>
# include <malloc.h>
struct