# 约瑟夫问题 数组解决

```#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. 2014-05-11 18:34:3821位水仙花数 by sxgkwei
2. 2014-05-19 17:12:34数据结构 顺序栈 by qqmmcc
3. 2014-05-22 11:38:42求两个数的最小公倍数 by 蟋蟀哥
4. 2014-05-22 15:43:22神经元模型 by lucasli
5. 2014-06-05 11:30:06经过优化后的快速排序算法 by 灵剑子
6. 2014-06-07 20:27:27解数独 by niutao.linux
7. 2014-06-15 19:46:43最小生成树，其中的普利姆算法我感觉很有范儿。 by aiheng1988
8. 2014-09-01 13:12:19计数排序 by lucasli
9. 2014-09-10 11:32:48快速排序代码（自己写的，有点冗余） by sxgkwei
10. 2014-10-27 10:07:37求一个数的所有质因子之和 by walker30
11. 2014-11-16 12:05:30通用排序程序 by lucasli