中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

php版的24點(diǎn)游戲源碼

2018-07-20    來(lái)源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬(wàn)Linux鏡像隨意使用
<?php
 
class TwentyFourCal {
    public  $needle = 24;
    public  $precision = '1e-6';
  
    function TwentyFourCal() {
    }
     
    private function notice($mesg) {
        var_dump($mesg);
    }
  
    /**
     * 取得用戶(hù)輸入方法
     */
    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) );
?>


標(biāo)簽:

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:iOS圖片縮小放大scollView實(shí)現(xiàn)代碼

下一篇:iOS隨機(jī)數(shù)的產(chǎn)生