-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path24.php
97 lines (91 loc) · 2.33 KB
/
24.php
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
91
92
93
94
95
96
97
<html>
<head>
<link rel='icon' href='/ico/24.ico'>
<link rel='apple-touch-icon' href='/ico/24.jpg'>
<title>24点</title>
</head>
<body>
<?php
$Tab4 = " ";
$seed = 200000;
srand(rand(1,time()-$seed*rand(1,50)));
for ($i=0; $i<=3; ++$i) {
srand(rand(1,$seed));
$value[] = rand(1,13);
};
set_time_limit(0);
$result = 24;
$list = array();
//$value=array(8,9,2,10);
makeValue($value);
$res = "无解";
if (count($list)>=1) {
$res = "有解";
}
?>
<table border='1' align='center'>
<tr>
<?php
for ($i=0; $i<=3; ++$i) {
echo $Tab4.$Tab4.$Tab4 . "<td width=20 align='center'>" . $value[$i] . "</td>" . "\n";
}
?>
</tr>
</table>
<div align='center'><?php echo $res;?></div>
</body>
</html>
<?php
// echo "<pre>";
// print_r($list);
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 ($start>0 && ($start+$len<count($set)))
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;
}
?>