Chinaunix首页 | 论坛 | 博客
  • 博客访问: 207429
  • 博文数量: 127
  • 博客积分: 1998
  • 博客等级: 上尉
  • 技术积分: 1432
  • 用 户 组: 普通用户
  • 注册时间: 2011-12-07 16:41
文章分类

全部博文(127)

文章存档

2014年(41)

2013年(1)

2012年(85)

分类: C/C++

2012-02-25 19:11:40

  1. /*
  2.  * =====================================================================================
  3.  *
  4.  * Filename: josephus.c
  5.  *
  6.  * Description:
  7.  * 用数组实现约瑟夫环 :
  8.  * 约瑟夫环问题的具体描述是:设有编号为1,2,……,n的n(n>0)个人围成一个圈,
  9.  * 从第1个人开始报数,报到m时停止报数,报m的人出圈,
  10.  * 再从他的下一个人起重新报数,报到m时停止报数,
  11.  * 报m的出圈,……,如此下去,直到只剩下一人为止。
  12.  * 当任意给定n和m后,设计算法求n个人出圈的次序。
  13.  *
  14.  * Version: 1.0
  15.  * Created: 02/24/2012 03:44:35 PM
  16.  * Revision: none
  17.  * Compiler: gcc
  18.  *
  19.  * Author: Mr. He Wei Ping (hwp), hwp195@gmail.com
  20.  * Company:
  21.  *
  22.  * =====================================================================================
  23.  */
  24. #include
  25. int main()
  26. {
  27.     int i;
  28.     int num, m, remain, count;

  29.     printf("Enter the number of peaple and the count: ");
  30.     scanf("%d %d", &num, &m);

  31.     int person[num];
  32.     for(i = 0; i < num; i ++)
  33.         person[i] = i + 1;

  34.     i = 0;
  35.     count = 1;
  36.     remain = num;
  37.     while(remain > 0)
  38.     {
  39.         if(person[i] != 0 && count != m)
  40.             count ++;
  41.         else if(person[i] != 0 && count == m)
  42.         {
  43.             printf("No%d out!\n", person[i]);
  44.             person[i] = 0;
  45.             count = 1;
  46.             remain --;
  47.         }
  48.         i ++;
  49.         if(i >= num)
  50.             i = i % num;
  51.     }
  52.     return 0;
阅读(652) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~