41个人排成一个圆圈,由第1个人开始报数,每报数到第3人就必须自杀,然后再由下一个重新报数,直到所有人都自杀身亡为止。
代码实现:
#include <stdil.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node * create(int n)
{
node *p=NULL,*head;
p=head = (node*)malloc(sizeof(node));
node *s;
int i=1;
if(0!=n)
{
while(i<=n)
{
s=(node*)malloc(sizeof(node));
s->data=i++;
p->next=s;
p=s;
}
s->next=head->next;
}
free(head);
}
int main()
{
int n = 41;
int m = 3;
int i;
node *p = creat(n);
node *temp;
m=n%m;
while()
{
for(i = 1;i<m;i++)
{
p = p->next;
}
printf("%d",p->next->data);
temp = p->next;
p->next= temp->next;
free(temp);
p=p->next;
}
printf("%d\n",p->data);
return 0;
}
浙公网安备 33010602011771号