ACM-josephus问题
View Code
1 #include<stdio.h> 2 #include<malloc.h> 3 typedef struct people 4 { 5 int num; 6 struct people *next; 7 }node; 8 void main() 9 { 10 node *head,*p1,*p2; 11 int n,m,i; 12 while(scanf( "%d", &n )==1) 13 { 14 head = p1 = (node *)malloc(sizeof(node)); 15 head->num = 1; 16 for( i=2;i<=n;i++ ) 17 { 18 p1->next = (node *)malloc(sizeof(node)); 19 p1 = p1->next; 20 p1->num=i; 21 } 22 p1->next=head; 23 scanf("%d",&m); 24 while(n!=1) 25 { 26 for(i=1; i<m;i++ ) 27 { 28 p1=p1->next; 29 } 30 p2=p1->next; 31 printf("%d ",p2->num ); 32 p1->next=p2->next; 33 free(p2); 34 n--; 35 } 36 printf("%dn",p1->num); 37 } 38 }