天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

php數字游戲 計算24算法

算法思路:把每一個數字看做一個獨立的數學表達式,表達式之間加上標點符號組合成新表達式,一共組合4次,表達式之間的所有組合可以通過遞歸來實現.
代碼如下:
復制代碼 代碼如下:
<?php
/**
* A 24 maker
* @version 1.0.0
* @author laruence<laruence at yahoo.com.cn>
* @copyright (c) 2009 http://www.laruence.com
*/
class TwentyFourCal {
public $needle = 24;
public $precision = '1e-6';
function TwentyFourCal() {
}
private function notice($mesg) {
var_dump($mesg);
}
/**
* 取得用戶輸入方法
*/
public function calculate($operants = array()) {
try {
$this->search($operants, 4);
} catch (Exception $e) {
$this->notice($e->getMessage());
return;
}
$this->notice('can/'t compute!');
return;
}
/**
* 求24點算法php實現
*/
private function search($expressions, $level) {
if ($level == 1) {
$result = 'return ' . $expressions[0] . ';';
if ( abs(eval($result) - $this->needle) <= $this->precision) {
throw new Exception($expressions[0]);
}
}
for ($i=0;$i<$level;$i++) {
for ($j=$i+1;$j<$level;$j++) {
$expLeft = $expressions[$i];
$expRight = $expressions[$j];
$expressions[$j] = $expressions[$level - 1];
$expressions[$i] = '(' . $expLeft . ' + ' . $expRight . ')';
$this->search($expressions, $level - 1);
$expressions[$i] = '(' . $expLeft . ' * ' . $expRight . ')';
$this->search($expressions, $level - 1);
$expressions[$i] = '(' . $expLeft . ' - ' . $expRight . ')';
$this->search($expressions, $level - 1);
$expressions[$i] = '(' . $expRight . ' - ' . $expLeft . ')';
$this->search($expressions, $level - 1);
if ($expLeft != 0) {
$expressions[$i] = '(' . $expRight . ' / ' . $expLeft . ')';
$this->search($expressions, $level - 1);
}
if ($expRight != 0) {
$expressions[$i] = '(' . $expLeft . ' / ' . $expRight . ')';
$this->search($expressions, $level - 1);
}
$expressions[$i] = $expLeft;
$expressions[$j] = $expRight;
}
}
return false;
}
function __destruct() {
}
}
/* demo */
$tf = new TwentyFourCal();
$tf->calculate( array(4,8,8,8) );
?>

php技術php數字游戲 計算24算法,轉載需保留來源!

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

主站蜘蛛池模板: 国产成人AV永久免费观看 | 色小说在线 | 日韩一本在线 | 我要搞av | 在线高清电影理论片4399 | 欧美国产在线一区 | 日韩中文字幕欧美在线视频 | 色综合伊人色综合网站中国 | 最新高清无码专区在线视频 | 漂亮的保姆3集电影免费观看中文 | 国内精品久久久久久西瓜色吧 | 俄罗斯大白屁股 | 精品视频一区二区三三区四区 | 中文中幕无码亚洲在线 | 九九夜夜妹子 | 2019伊人查蕉在线观看 | 成人高清护士在线播放 | 伊人久久精品99热超碰 | 日韩精品欧美在线视频在线 | 综合色中色| 各种肉黄浪荡故事集 | 99视频在线免费观看 | 国产色精品久久人妻无码看片 | 色播播影院 | 在线精品国精品国产不卡 | 成人毛片18岁女人毛片免费看 | 男女疯狂一边摸一边做羞羞视频 | 国产成人在线视频播放 | 国产中文字幕一区 | 国产福利视频第一导航 | TUBE69CHINESE学生 TUBE19UP老师学生 | 亚洲AV噜噜88 | java农村野外妇女hd | 亚洲欧美成人综合 | 性VIDEOSTV另类极品 | 无人区日本电影在线观看高清 | 香蕉久久夜色精品国产小优 | 九九热这里都是精品 | 亚洲综合视频 | 闺蜜扒开我尿口使劲揉 | 国产精华av午夜在线观看 |