约瑟夫环问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include <stdio.h> #include <stdlib.h> typedef struct node* link; struct node{int item;link next;}; int main(int argc,char *argv[]){ int i,N=atoi(argv[1]),M=atoi(argv[2]); link t=malloc(sizeof *t),x=t; t->item=1; t->next=t; for (i=2;i<=N; i++) { x=(x->next=malloc(sizeof *x)); x->next=t; x->item=i; } while (x!=x->next) { for (i=1;i<M; i++) x=x->next; x->next=x->next->next; N--; } printf("%d\n",x->item); return 0; } |