search
Homephp教程php手册php计算24点游戏

求: 三个5,一个1组成的数学表达式的结果=24,   结果:(5-1/5)*5=24

 set_time_limit(0);
 $values = array(1, 5, 5, 5);
 $result = 24;

 $list = array();

 echo "

";<br> makeValue($values);<br> print_r($list);<p> function makeValue($values, $set=array())<br> {<br>  $words = array("+", "-", "*", "/");<br>  if(sizeof($values)==1)<br>  {<br>   $set[] = array_shift($values);<br>   return makeSpecial($set);<br>  }<br>  foreach($values as $key=>$value)<br>  {<br>   $tmpValues = $values;<br>   unset($tmpValues[$key]);<br>   foreach($words as $word)<br>   {<br>    makeValue($tmpValues, array_merge($set, array($value, $word)));<br>   }<br>  }<br> } <br> <br> function makeSpecial($set)<br> {<br>  $size = sizeof($set);</p><p>  if($size  {<br>   return makeResult($set);<br>  }</p><p>  for($len=3; $len  {<br>   for($start=0; $start   {<br>    if(!($set[$start-1]=="*" || $set[$start-1]=="/" || $set[$start+$len]=="*" || $set[$start+$len]=="/"))<br>     continue;<br>    $subSet = array_slice($set, $start, $len);<br>    if(!in_array("+", $subSet) && !in_array("-", $subSet))<br>     continue;<br>    $tmpSet = $set;<br>    array_splice($tmpSet, $start, $len-1);<br>    $tmpSet[$start] = "(".implode("", $subSet).")";<br>    makeSpecial($tmpSet); <br>   }<br>  }<br> } </p><p><br> function makeResult($set)<br> {<br>  global $result, $list;<br>     $str = implode("", $set);<br>  @eval("\$num=$str;");<br>  if($num==$result && !in_array($str, $list))<br>   $list[] = $str;<br> } </p><p>?><br></p>
            <p style="width:100%;text-align:center;margin:10px 0">

<br>
<br>

</p><p style="width:100%;text-align:center;margin:10px 0">

            </p><p class="clear"></p>

           
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool