PHP数3退1対象向けの書き方
3597 ワード
<?php
// 3 1
class Monkey {
/**
*
* $id
* $left
* $right
*/
public $id;
public $left;
public $right;
}
class Circle{
/**
*
* $ct
* $first
* $last
*/
public $ct = 0;
public $first;
public $last;
//
//
function __construct($c){
for($i=0;$i<$c;$i++){
$this->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;
?>