- #include<stdio.h> //一圈共有N个人,开始报数,报到M的人自杀,然后重新开始报数
- #include<stdlib.h>
- typedef struct list
- {
- int data;
- struct list *next;
- }node;
- node *create(int N)
- {
- int i;
- node *head,*p1,*p2;
- int a;
- head=NULL;
- printf("输入节点值: ");
- for(i=N;i>0;i--)
- {
- p1=(node *)malloc(sizeof(node));
- scanf("%d",&a);
- p1->data=a;
- if(head==NULL)
- {
- head=p1;
- p2=p1;
- }
- else{
- p2->next=p1;
- p2=p1;
- }
- }
- p2->next=head;
- return head;
- }
- node *del(node *head,int M)
- {
- node *r,*t,*p;
- p=head;
- int i=2;
- while(i<M)
- {
- p=p->next;
- i++;
- }
- r=p;
- t=p->next;
- r->next=t->next;
- free(t);
- return r->next;
- }
- int main()
- {
- node *p,*q,*r,*t;
- int j,M,N;
- printf ("输入M,N: ");
- scanf("%d %d",&M,&N);
- j=N;
- r=p=create(N);
- while(j!=1)
- {
- q=del(p,M);
- p=NULL;
- p=q;
- j--;
- }
- printf("最后一个死的是:%d\n",q->data);
- return 0;
- }
来源: https://www.oschina.net/code/snippet_2879488_58551