<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<?php
class child{
public $no;
public $next=null;
public function __construct($no){
$this->no=$no;
}
}
function add(&$frist,$n=4){
for($i=0;$i<$n;$i++){
$child= new child($i+1);
if($i==0){
$frist=$child;
$frist->next=$child;
$cur=$frist;
}
else{
$cur->next=$child;
$child->next=$frist;
$cur=$cur->next;
}
}
}
function showchild($frist){
$cur=$frist;
while($cur->next!=$frist){
echo $cur->no."xiaohaizi<br>";
$cur=$cur->next;
}
echo $cur->no."xiaohaizi<br>";
}
$m=2;
function countchild($frist,$m=2){
$tail=$frist;
while($tail->next!=$frist){
$tail=$tail->next;
}
while($tail!=$frist){
for($i=0;$i<($m-1);$i++){
$tail=$tail->next;
$frist=$frist->next;
};
echo'<br/>出圈的人的编号是'.$frist->no;
$frist=$frist->next;
$tail->next=$frist;
}
echo "<br/>最后剩下人的编号".$tail->no;
}
$frist=null;
add($frist,$n=100);
showchild($frist);
countchild($frist);
?>
http://blog.youkuaiyun.com/wenximalong/article/details/8299432
转载于:https://blog.51cto.com/xiaochaozi/1401376