#include <iostream>
#include <string>
using namespace std;
#define LIVES 30
#define DEADS 15
#define DEAD_NUM 9
void SolutionByArray()
{
static int died[LIVES];
int i, j, cur = 0;
for (i = 0; i < DEADS; i++)
{
for (j = 0; j < DEAD_NUM; j += 1 - died[cur++])
{
if (cur == LIVES)
cur = 0;
}
cout << "第" << cur << "号下船了" << endl;
died[cur - 1] = 1;
}
}
struct person
{
unsigned id;
struct person *next;
}persons[LIVES], *p, *before;
void SolutionByLinkedList()
{
int i, j;
for (i = 0; i < LIVES; i++)
{
persons[i].id = i + 1;
persons[i].next = persons + i + 1;
}
persons[LIVES - 1].next = persons;
p = persons + LIVES - 1;
for (i = 0; i < DEADS; i++){
for (j = 0; j < DEAD_NUM - 1; j++)
{
p = p->next;
}
before = p, p = p->next;
cout << "第" << p->id << "号下船了" << endl;
before->next = p->next;
}
}
void SolutionByRecursion()
{
int i, j, id;
for (i = 0; i < DEADS; i++){
for (j = i, id = -1; j >= 0; j--){
id = (id + DEAD_NUM) % (LIVES - j);
}
cout << "第" << id + 1 << "号下船了" << endl;
}
}
int main()
{
SolutionByRecursion();
system("PAUSE");
return 0;
}