先定义一个循环链表的结构体
struct mystructure
{
int id = 0;//添加id的属性
int live = 1;//生命值为1代表生存
mystructure* next = nullptr;
};
定义头尾指针并生成一个指定格数为p的循环链表
mystructure* head = new mystructure;
mystructure* tail = new mystructure;
head->id = 1;
head->next = tail;
tail->id = 2;
for (int i = 3; i <= p; i++) {
mystructure* xin = new mystructure;
xin->id = i;
tail->next = xin;
tail = xin;
}
tail->next = head;//最后的尾指针指向头,形成循环
生成一个用于循环的指针使其等于头节点
mystructure* newpoint = new mystructure;
newpoint = head;//从头结点开始遍历循环
定义遍历的结束位置
int i = 1;
int j = 0;
int the_middle = (p / 2) * c;
int the_end = p * c;
函数实现主体
while (i <= the_end)
{
if (newpoint->live == 1) {
if (j == 1) {//这是从已死亡的人移过来的
i++;
if (i % c == 0) {
cout << newpoint->id << "--" << i << "--" << i / c << endl;//输出死亡id
newpoint->live = 0;
newpoint = newpoint->next;
if (newpoint->live == 1) {
i++;
}
}
j = 0;
}
else {
if (i % c == 0) {
cout << newpoint->id << "--" << i << "--" << i / c << endl;
newpoint->live = 0;
if (newpoint->next->live == 1) {
i++;
}
newpoint = newpoint->next;
}
if (newpoint->next->live == 1) {
i++;
}
newpoint = newpoint->next;
}
}
else {
newpoint = newpoint->next;
j = 1;
}
}
定义总人数为41个,每3人一循环:
3--6--9--12--15--18--21--24--27--30--33--36--39--1--5--1--14--19--23--28--32--37--41--7--13--20--26--34--40--8--17--29--38--11--25--2--22--4--35--16--31
原文:https://www.cnblogs.com/Kevin-H/p/12005441.html