# 约瑟夫问题 数组解决

#include <stdio.h>
#include <stdlib.h>

#define TOTAL 6

struct person {
char    name[10];
int     count;
};

int main()
{
struct person p[TOTAL] = {
{"A", 4},
{"B", 2},
{"C", 3},
{"D", 5},
{"E", 1},
{"F", 2},
};

int current = 0;
int count = 0;
int alive = TOTAL;
int r = 0;
int i, j;

r = random() % TOTAL;
printf("random number: %d\\n", r);
current = (current + r -1) % TOTAL;

while(alive > 0)
{
printf("begin with %s, count: %d\\n", p[current].name, p[current].count);

//out = (current + p[current].count) % TOTAL;
count = p[current].count;
for(j=0; j<count-1; ++j)
{
current = (current + 1) % TOTAL;
while(p[current].count == 0)
current = (current + 1) % TOTAL;
}
printf("%s is out\\n", p[current].name);
p[current].count = 0;
--alive;

if(alive > 0)
{
current = (current + 1) % TOTAL;
while(p[current].count == 0)
current = (current + 1) % TOTAL;
}
}

return 0;
}
//该片段来自于http://outofmemory.cn

0人收藏

0

0

»更多 您可能感兴趣的代码
1. 2013-07-17 19:11:32c++动态规划算法计算网络的最长路线和最短路线 by lucasli
2. 2014-02-12 11:56:53C++算法之数星星 by lucasli
3. 2014-02-21 12:23:12C++实例解析哈夫曼树 by lucasli
4. 2014-03-30 15:29:42C++算法之链表排序 by 跳跳虎
5. 2014-04-24 17:42:39C++算法之链表重合 by lucasli
6. 2014-05-20 10:42:08双向循环列表 by Kevin.
7. 2014-05-27 14:13:38网络中两点最短路径 Dijkstra 算法 by 千万不要郁闷
8. 2014-06-08 10:40:28数字的全排列 by sxgkwei
9. 2014-06-24 14:17:58C++ 算法之单词统计 by 小项
10. 2014-07-06 21:23:00死锁检测算法 by 童学芬
11. 2014-07-20 12:34:50傻瓜递归 by 童学芬

1. bystander 发表 2013-04-16 00:42:58 模板优先级队列及堆排序(C++实现)
2. dianlujitao 发表 2014-10-17 13:52:22 POJ 2388 Who’s in the Middle
3. surgesoft 发表 2014-10-28 08:01:58 LeetCode OJ: Restore IP Addresses
4. espace 发表 2015-07-18 17:43:14 Two Sum
5. abyssss 发表 2014-05-20 03:23:39 数据结构 最小堆 数组实现
6. dianlujitao 发表 2014-10-17 13:56:48 POJ 1611 The Suspects
7. bystander 发表 2013-05-15 10:37:24 倒水问题求解(C++)
8. dianlujitao 发表 2014-10-17 14:11:26 POJ 1328 Radar Installation
9. bystander 发表 2013-04-01 10:12:37 [藏]关于B树的一篇文章
10. lvfuyu 发表 2015-04-12 08:53:30 [hihocoder]矩阵快速幂
11. lvfuyu 发表 2015-04-18 09:13:32 [hihocoder]二分查找
12. dianlujitao 发表 2013-11-04 11:49:12 【NOIP2012提高组】Vigenère密码 模拟 打表