//assuming that there are N people here in a circle and every Mth people is killed untill the last man. //Then who will be alive. //This program is aimed to solve this problem. #include<stdio.h> #include<stdlib.h> typedef struct node* link; struct node { int item; link next; }; main() { int N; int M; int i=1; link t=malloc(sizeof *t); link x=t; link temp; scanf("%d %d",&N,&M); t->item=1; t->next=t; for(i=2;i<=N;i++) { x->next=malloc(sizeof *x); x=x->next; x->item=i; x->next=t; } //right now the pointer x is pointing the last one of the list and make sure of it while(x!=x->next) { for(i=1;i<M;i++) { x=x->next; } temp=x->next; printf("%3d",temp->item); x->next=x->next->next; free(temp); //It is very important to free the one which is deleted } printf("\n%d\n",x->item); } //下面是用链表实现约瑟夫问题的方法: //main() #include<stdio.h> #include<stdlib.h> #include"head.h" main() { int i; int N=9; int M=5; link temp; link x=initial_circle(N); while(x!=x->next) { for(i=1;i<M-1;i++) { x=x->next; } temp=x->next; x->next=x->next->next; free(temp); x=x->next; } printf("%d",x->item); free(x); } //头文件 #ifndef _HEAD_H_ #define _HEAD_H_ typedef struct node* link; struct node { int item; link next; }; extern link reverse(link x); extern void print(link x); extern link initial(int N); extern link put_ahead(link x,int n); extern void insert(link m,link n,int number); extern link put_last(link x,int n); extern link sort(link x); extern link another_sort(link x); extern void freenode(link x); extern link initial_circle(int N); #endif #ifndef _HEAD_H_ //函数 其中一些并没有用到 #include"head.h" #include<stdio.h> #include<stdlib.h> link reverse(link x) { link y; link t; link r; r=NULL; y=x; while(y!=NULL) { t=y->next; y->next=r; r=y; y=t; } return r; } void print(link x) { while(x!=NULL) { printf("%d\t",x->item); x=x->next; } printf("--------------------------------------------\n"); } link initial(int N) { int i; link x; link t; x=(t=malloc(sizeof *t)); for(i=1;i<N;i++) { t->item=rand()%N; t->next=malloc(sizeof *t); t=t->next; } t->item=rand()%N; t->next=NULL; return x; } link put_ahead(link x,int n) { link t; link temp; t=malloc(sizeof *t); t->item=n; temp=x; x=t; t->next=temp; return x; } void insert(link m,link n,int number) { link temp=malloc(sizeof *temp); temp->item=number; temp->next=n; m->next=temp; } link put_last(link x,int n) { link t=x; link temp=malloc(sizeof *temp); temp->item=n; while(t->next!=NULL) { t=t->next; } temp->next=NULL; t->next=temp; return x; } link sort(link x) { link y; link m; int flag; y=malloc(sizeof *y); y->item=x->item; y->next=NULL; x=x->next; while(x!=NULL) { m=y; flag=1; if(x->item<= y->item) { y=put_ahead(y,x->item); } else { while(m->next!=NULL) { if( !((x->item<m->item) || (x->item>m->next->item))) { insert(m,m->next,x->item); flag=0; break; } else { m=m->next; } } if(flag) { y=put_last(y,x->item); } } x=x->next; } return y; } link another_sort(link x) { link y=malloc(sizeof *y); link y_temp; link x_temp; link yy; int flag; y->next=x; x=x->next; y->next->next=NULL; while(x!=NULL) { yy=y; y_temp=y->next; while(y_temp!=NULL) { if(x->item<=y_temp->item) { yy->next=x; x_temp=x; x=x->next; x_temp->next=y_temp; flag=1; yy=yy->next; y_temp=y_temp->next; break; } flag=0; yy=yy->next; y_temp=y_temp->next; } if(!flag) { yy->next=x; x_temp=x; x=x->next; x_temp->next=NULL; } } return y->next; } void freenode(link x) { while(x!=NULL) { free(x); x=x->next; } printf("x has been freed!\n"); } link initial_circle(int N) { int i; link x=malloc(sizeof *x); link y=x; for(i=1;i<N;i++) { y->item=i; y->next=malloc(sizeof *y); y=y->next; } y->item=N; y->next=x; return x; }
约瑟夫问题的C语言求解
最新推荐文章于 2023-04-28 17:53:54 发布