php约瑟夫环

<?php 
function popChar($str , $m , $current = 0){
    $number = count($str);
    $num = 1;
    if(count($str) == 1){
        echo $str[0];
        return;
    }
    else{
        while($num++ < $m){
            $current++ ;
            $current = $current%$number;
        }
        echo $str[$current]."<br/>";
        array_splice($str , $current , 1);
        popChar($str , $m , $current);
    }
}
//$str = array(1 , 2 , 3 , 4 , 5 , 6 , 7, 8 , 9 , 10);  
$str = array('a','b','c','d','e','f','g','h');  
$m = 4; 
popChar($str , $m);
?>

 

posted on 2015-05-28 12:30  Shower稻草人  阅读(109)  评论(0编辑  收藏  举报