|
|
#2
hzh5122010-05-06 13:37
简单的约瑟夫环,楼主这也要别人写呀!
程序代码:#include <stdio.h> #include <stdlib.h> typedef struct list { int num ; struct list *next ; }LIST ; int main(void) { LIST *head=NULL,*pre=NULL,*post=NULL, *start=NULL, *temp=NULL; long i, m, n, x; printf("n="); scanf("%ld",&n); printf("\nm="); scanf("%ld",&m) ; printf("\n"); if( n <= 1) { printf("1 "); return 0; } /**//*建立循环链表*/ pre = (LIST *)malloc(sizeof(LIST)); pre->num = 1 ; pre->next = NULL ; head = pre ; post = pre ; for(i=2 ; i<= n; i++) { pre = (LIST *)malloc(sizeof(LIST)); pre->num = i; pre->next= NULL ; post->next=pre ; post = pre ; } post -> next = head ; /**//*将最后一个结点的指向头,这样就构成了循不链表*/ pre= head ; i--; while(i>0) { printf(" %d ",pre->num); pre=pre->next; i--; } printf("\n"); printf("\n从第几个人开始:"); scanf("%d",&x); int count=n; while(count>0) { if(head->num==x) { pre=post; start=head; break; } if(pre->next->num==x) { start=pre->next; break; } pre=pre->next; count--; } if(count==0) { printf("\n 开始人错误\n"); system("pause"); exit(1); } while (start->next != start) { for(i=1; i < m ; i++) { pre = start ; start = start->next; } temp=start; start=start->next; pre->next=start; delete temp; } printf("last=%d \n",pre->num); return 0 ; } |
[问题描述]设有n个人围坐一圈,现从某个人开始报数,数到m的人出列,接着从下一个人开始重新开始报数,数到m的人又出列,如次下去,直到所有的人都出列为止。试设计确定他们出列的顺序的程序
1)在顺序存储结构上实现以上过程。
2)在循环链表上实现以上过程。
程序代码:
真受不了!