數三退一問題---php
<?php
class kid {
public $left;
public $right;
public $id;
function __construct($f) {
$this->id=$f;
}
}
$num;
$first;
$last;
function circle ($m,$n) {
$num=$m;
for ($i = 1; $i <=$m; $i++) {
$k=new kid($i);
echo"當前的ID為".$k->id."</br>";
echo "</br>";
// print_r($k); echo "</br>";
if($i==1){
$first=$last=$k;
}elseif($i==2){
$last=$k;
$first->left=$last;
$first->right=$last;
$last->left=$first;
$last->right=$first;
// echo "<pre/>";
// print_r($last);
}else{
$last->right=$k;
$k->right=$first;
$k->left=$last;
$first->left=$k;
$last=$k;
}
}
$flag=1;
$now=$first;
while(($now->right)!==$now) {
if($flag!=$n) {
$flag++;
} else {
$now->left->right=$now->right;
$now->right->left=$now->left;
$flag=1; echo $now->id."號退出!</br>";
}
$now=$now->right;
}
echo "最後的人的ID為:$now->id";
}
circle(12,3);
?>