约瑟夫环
#include"stdio.h"
struct node
{int n;
struct node *next;
}
main()
{int i,j,b=0,N,m,s;
struct node *p,*head,*q;
printf("input three numbers:\n");
scanf("%d %d %d",&m,&N,&s);
head=(struct node*)malloc(sizeof(struct node));
head->n=s;
head->next=head;
for(i=m;i>0;i--)
{p=(struct node*)malloc(sizeof(struct node));
p->next=head->next;
p->n=i;
head->next=p;
}
while(p->next!=head)
p=p->next;
p->next=head->next;
for(i=1;i<s;i++)
p=p->next;
for(i=0;i<m;i++)
{
for(j=1;j<N;j++)
p=p->next;
q=p->next;
p->next=q->next;
printf("%4d",q->n);
free(q);
}
}
struct node
{int n;
struct node *next;
}
main()
{int i,j,b=0,N,m,s;
struct node *p,*head,*q;
printf("input three numbers:\n");
scanf("%d %d %d",&m,&N,&s);
head=(struct node*)malloc(sizeof(struct node));
head->n=s;
head->next=head;
for(i=m;i>0;i--)
{p=(struct node*)malloc(sizeof(struct node));
p->next=head->next;
p->n=i;
head->next=p;
}
while(p->next!=head)
p=p->next;
p->next=head->next;
for(i=1;i<s;i++)
p=p->next;
for(i=0;i<m;i++)
{
for(j=1;j<N;j++)
p=p->next;
q=p->next;
p->next=q->next;
printf("%4d",q->n);
free(q);
}
}
浙公网安备 33010602011771号