php解决约瑟夫环的问题

php里面解决约瑟夫环还是比较方面的,但是下面的方法太费空间

<?php 
class SelectKing{
	private $m;//幅度
	private $n;//总数
	
	public function __construct($m,$n){
		$this->m = $m;
		$this->n = $n;
	}
	
	public function getKing(){
		$mokeys = range(1, $this->n);
		$tmp = 0;
		while(count($mokeys)>1){
			$tmp++;
			$head = array_shift($mokeys);
			if($tmp%$this->m!=0){
				array_push($mokeys, $head);
			}
		}
		return $mokeys;
	}
}

class Client{
	public static function main(){
		$obj = new SelectKing(3, 10);
		$king = $obj->getKing();
		var_dump($king);
	}
}

Client::main();
?>

  

posted @ 2014-12-08 19:25  tai君  阅读(383)  评论(0编辑  收藏  举报