首页 > 编程语言 > 详细

约瑟夫环-循环队列算法

时间:2015-10-08 00:26:38      阅读:680      评论:0      收藏:0      [点我收藏+]

技术分享

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define NUM 5  //游戏人数
 4 #define OUT 3 // 逢3出局
 5 struct Person
 6 {
 7     int num;
 8     struct Person *next;
 9 };
10 int main()
11 {   int i,j;
12     int cnt=1;
13     int index=0;
14     int c[NUM]={0};
15     struct Person person[NUM],*p;
16     printf("\n游戏人数是 %d 人\n逢 %d 出局\n\n",NUM,OUT);
17     for(i=0;i<NUM;i++)
18     {   
19         person[i].num=i+1;
20         person[i].next=&person[(i+1)%NUM];
21     }
22     p=&person[0];
23     while((p->num)!=(p->next->num))
24     {
25         while(cnt<OUT-1)
26         {
27             p=p->next;
28             cnt++;
29         }
30         c[index]=p->next->num;
31         index++;
32         p->next=p->next->next;
33         p=p->next;
34         cnt=1;
35     }
36     c[index]=p->num;
37     printf("最后一个人是:%d\n\n",p->num);
38     printf("出局的顺序是:\n");
39     for(j=0;j<NUM;j++)
40     {
41         printf("-->%d",c[j]);
42     }
43     printf("\n\n");
44     system("pause");
45     return 0;
46 }

 

约瑟夫环-循环队列算法

原文:http://www.cnblogs.com/houjun/p/4859876.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!