约瑟夫问题
<?php
function yoseph($all,$number){
/*
名称 约瑟夫问题函数
作用 依次打印退出者序号
备注 起始序号为1
用法 yoseph(19,7)
参数1 所有人数
参数2 退出者序数
性能 参数1小于10000在1秒内响应
输出 依次打印退出者序号
返回 无
版权 署名
日期 2010-5-31
版本 第二版
作者 崔凯
联系 cuikai.chn@gmail.com
网址 http://cuikai-wh.com/
感谢 刘晓
历史 2010-06-01 在最后一个循环的判断里,增加break语句
*/
for($i=0; $i<$all; $i++){
$member[$i] = 1;
}
$numCount = 0;
$count = $all;
while( 1< $count){
for($i=0; $i<$all; $i++){
if(1 == $member[$i]){
++$numCount;
}
if($numCount == $number){
$member[$i] = 0;
echo ($i+1).'<br />'; //依次打印数列
$numCount = 0;
$count = $count-1;
}
}
}
for($i=0; $i<$all; $i++){
if(1 == $member[$i]){
echo '<br />'.($i+1); //打印最后一位
break;
}
}
}
?>
[...] 感谢 刘晓 历史 2010-更多… View full post on 时光立方 [...]
2011-05-25 20:46