http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2056&cid=1156
1 #include<stdio.h> 2 #include<stdlib.h> 3 typedef struct node 4 { 5 int data; 6 struct node *next; 7 }lb; 8 lb *creat(int n) 9 { 10 int i; 11 lb *p,*head,*tail; 12 p=(lb *)malloc(sizeof(lb)); 13 p->data=1; 14 p->next=NULL; 15 head=p; 16 tail=p; 17 for(i=2;i<=n;i++) 18 { 19 p=(lb *)malloc(sizeof(lb)); 20 p->data=i; 21 tail->next=p; 22 tail=p; 23 p->next=NULL; 24 } 25 tail->next=head; 26 return head; 27 } 28 void sel(lb *head,int n) 29 { 30 int k=0; 31 int l=0; 32 lb *q,*p; 33 q=head; 34 while(q->next!=head) 35 q=q->next; 36 while(l<n-1) 37 { 38 p=q->next; 39 k++; 40 if(k%5==0) 41 { 42 q->next=p->next; 43 if(p->data==1)break; 44 free(p); 45 l++; 46 } 47 else q=p; 48 49 } 50 if(n==5) 51 printf("1\n"); 52 else 53 printf("%d\n",l+1); 54 55 } 56 int main() 57 { 58 int n; 59 lb *head; 60 while(~scanf("%d",&n)&&n) 61 { 62 head=creat(n); 63 sel(head,n); 64 } 65 return 0; 66 }
欢迎来访