2056=不敢死队问题

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 struct node
 4 {
 5     int data;
 6     struct node*next;
 7 };
 8 int main()
 9 {
10     int m,n,x,i,j;
11     while(~ scanf("%d",&n)&&n!=0)
12     {
13         struct node*head,*p,*end;
14         head=(struct node*)malloc(sizeof(struct node));
15         head->next=NULL;
16         end=head;
17         head->data=1;//因为每次都是一个新的循环,所以都要重置,否则第二次循环会出错误。
18         for(i=0; i<n-1; i++)
19         {
20             p=(struct node*)malloc(sizeof(struct node));
21             p->data=i+2;
22             p->next=head;//环状问题,让p的下一个等于head。
23             end->next=p;
24             end=p;
25         }
26         for(j=1; j<n; j++)
27         {
28             if(j==n-1)printf("%d\n",n);//如果只剩排长了,那么直接输出。
29             p=end->next;
30             for(i=1; i<=4; i++)
31             {
32                 p=p->next;
33                 end=end->next;//循环次数要比m小一个,以便于下面进行删除操作。
34             }
35             if(p->data==1)
36             {
37                 printf("%d\n",j);
38                 break;
39             }
40             end->next=p->next;//和约瑟夫一样的删除操作。
41         }
42     }
43     return 0;
44 }

 

posted @ 2019-03-06 19:42  念文丶  阅读(395)  评论(0编辑  收藏  举报