php计算24点游戏

修改了下,加了个表单。原文转载自 http://www.alixixi.com/program/a/2008050731708.shtml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
set_time_limit(0);
?>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
<meta http-equiv="pragram" content="no-cache">
<meta http-equiv="cache-control" content="no-cache, must-revalidate">
<meta http-equiv="expires" content="0">
<meta http-equiv="Content-Type" content="text/HTML; charset=utf-8">
<title>24 点计算器</title>
</head>
<body>
<?php
$values = explode(' ',$_POST['num']);
$result = 24;

$list = array();

if ($_POST) {
makeValue($values);
echo '<pre>';
print_r($list);
echo '</pre>';
}

function makeValue($values, $set=array())
{
$words = array("+", "-", "*", "/");
if(sizeof($values)==1)
{
$set[] = array_shift($values);
return makeSpecial($set);
}

foreach($values as $key=>$value)
{
$tmpValues = $values;
unset($tmpValues[$key]);
foreach($words as $word)
{
makeValue($tmpValues, array_merge($set, array($value, $word)));
}
}
}

function makeSpecial($set)
{
$size = sizeof($set);

if($size<=3 || !in_array("/", $set) && !in_array("*", $set))
{
return makeResult($set);
}

for($len=3; $len<$size-1; $len+=2)
{
for($start=0; $start<$size-1; $start+=2)
{
if(!($set[$start-1]=="*" || $set[$start-1]=="/" || $set[$start+$len]=="*" || $set[$start+$len]=="/"))
continue;
$subSet = array_slice($set, $start, $len);
if(!in_array("+", $subSet) && !in_array("-", $subSet))
continue;
$tmpSet = $set;
array_splice($tmpSet, $start, $len-1);
$tmpSet[$start] = "(".implode("", $subSet).")";
makeSpecial($tmpSet);
}
}
}

function makeResult($set)
{
global $result, $list;
$str = implode("", $set);
@eval("\$num=$str;");
if($num==$result && !in_array($str, $list))
$list[] = $str;
}

?>
<center>
<form action="" method="post">
<p><label>请输入四个数字,空格分割:<br /><input type="text" name="num" /></label></p>
<p><input type="submit" name="submit" value="提交" /></p>
</form>
</center>
</body>
</html>