本文主要是介绍数据结构 杀人游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在一艘船上有被1到30号编号的同学,他们按顺时针围坐成一个圈。假定从1号同学开始顺时针计数,计数逢满7,遍把该位同学杀害并把他踢下船。然后游戏从被杀害的同学的下一位进行新一轮的“从零开始计数,逢7便杀”的游戏规则,直到杀完为止。按顺时针,周而复始。
#include<iostream>
using namespace std;
class people
{int life;int number;
public:people *next;people(){life=1;number=0;next=NULL;}void setnumber(int i){number=i;}void setnext(people* P){next=P;}void setlife(bool j){life=j;}int disnumber(){return number;}int dislife(){return life;}
};
class quan
{
public:people *head;people *last;quan(){head=last=new people;head->setnumber(1);}void addpeople(int Number){people* People=new people;last->setnext(People);last=People;last->setnext(head);last->setnumber(Number);}
};
int Q=1,allpeople=30;
void main()
{quan Quan;for(int i=2;i<=30;i++){Quan.addpeople(i);}people *Peo=Quan.last;while(allpeople){ // Q++;Peo=Peo->next;if(Peo->dislife()){{if(Q%7==0){Peo->setlife(0);cout<<Peo->disnumber()<<"号已死"<<endl;Q=1;allpeople--;}else{Q++;}}//allpeople--;}}}
运行结果。
这篇关于数据结构 杀人游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!