php數(shù)字游戲 計(jì)算24算法
更新時(shí)間:2012年06月10日 18:39:46 作者:
輸入任意4個(gè)數(shù)字,然后對(duì)其進(jìn)行+-*/組合,所得數(shù)學(xué)表達(dá)式值等于24
算法思路:把每一個(gè)數(shù)字看做一個(gè)獨(dú)立的數(shù)學(xué)表達(dá)式,表達(dá)式之間加上標(biāo)點(diǎn)符號(hào)組合成新表達(dá)式,一共組合4次,表達(dá)式之間的所有組合可以通過(guò)遞歸來(lái)實(shí)現(xiàn).
代碼如下:
<?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點(diǎn)算法PHP實(shí)現(xiàn)
*/
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) );
?>
代碼如下:
復(fù)制代碼 代碼如下:
<?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點(diǎn)算法PHP實(shí)現(xiàn)
*/
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) );
?>
相關(guān)文章
PDO預(yù)處理語(yǔ)句PDOStatement對(duì)象使用總結(jié)
這篇文章主要介紹了PDO預(yù)處理語(yǔ)句PDOStatement對(duì)象使用總結(jié),本文介紹了PDOStatement的方法及常用方法的使用例子,需要的朋友可以參考下2014-11-11基于Laravel 5.2 regex驗(yàn)證的正確寫法
今天小編就為大家分享一篇基于Laravel 5.2 regex驗(yàn)證的正確寫法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-09-09ThinkPHP使用Smarty第三方插件方法小結(jié)
這篇文章主要介紹了ThinkPHP使用Smarty第三方插件方法,結(jié)合實(shí)例形式總結(jié)分析了ThinkPHP使用Smarty模板的具體步驟與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-03-03ThinkPHP自定義Redis處理SESSION的實(shí)現(xiàn)方法
這篇文章主要介紹了ThinkPHP自定義Redis處理SESSION的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了ThinkPHP相關(guān)數(shù)據(jù)庫(kù)配置與自定義Redis處理session的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-05-05Laravel 手動(dòng)開(kāi)關(guān) Eloquent 修改器的操作方法
這篇文章主要介紹了Laravel 手動(dòng)開(kāi)關(guān) Eloquent 修改器的操作方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12用PHP寫的一個(gè)冒泡排序法的函數(shù)簡(jiǎn)單實(shí)例
下面小編就為大家?guī)?lái)一篇用PHP寫的一個(gè)冒泡排序法的函數(shù)簡(jiǎn)單實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05Thinkphp5.0框架使用模型Model的獲取器、修改器、軟刪除數(shù)據(jù)操作示例
這篇文章主要介紹了Thinkphp5.0框架使用模型Model的獲取器、修改器、軟刪除數(shù)據(jù)操作,結(jié)合實(shí)例形式分析了thinkPHP5.0模型Model獲取器、修改器數(shù)據(jù)操作相關(guān)實(shí)現(xiàn)技巧與注意事項(xiàng),需要的朋友可以參考下2019-10-10