时间:2021-07-01 10:21:17 帮助过:3人阅读
add(); } } //猴子的添加方法 function add(){ $m = new Monkey(); $m->id = $this->ct; if($this->ct==0){ $this->first = $m; $this->last = $m; $m->left = $m; $m->right = $m; }else{ $this->last->right = $m; $m->left = $this->last; $m->right = $this->first; $this->first->left = $m; $this->last = $m; } $this->ct++; } //猴子的删除方法 function del($m){ if($this->ct <=0){ return; }else if($this->ct == 1){ $this->first = $this->last = null; }else{ $m->left->right = $m->right; $m->right->left = $m->left; if($m == $this->first){ $this->first = $m->right; }else if($m == $this->last){ $this->last = $m->left; } } $this->ct--; } } /* 这里的代码就是实现让500只猴子围城一圈然后 数三个退一个就这样直到还剩一个把那猴子的编号打印出来 */ $circle = new Circle(500); $number = 0; $mk = $circle->first; while($circle->ct > 1){ $number++; if($number == 3){ $number = 0; $circle->del($mk); } $mk = $mk->right; } echo $circle->first->id; ?>