-
Notifications
You must be signed in to change notification settings - Fork 0
/
24.php
114 lines (104 loc) · 2.28 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
if(count($argv) != 5) die('参数异常');
array_shift($argv);
$result = permutation($argv);
$result = getExpression($result);
$result = array_unique($result);
$out = [];
foreach ($result as $v)
{
$str = "return {$v};";
$result = @eval($str);
if(round($result, 8) == 24) $out[] = substr($str, 7);
}
print_r($out);
die;
/**
* 获取所有表达式
* @param array $data
* @return array
*/
function getExpression($data)
{
// 情况1:两两分组
$rs = [];
foreach($data as $v)
{
$a = getAllResult($v[0], $v[1]);
$b = getAllResult($v[2], $v[3]);
foreach($a as $v1)
{
foreach($b as $v2)
{
$tmp = getAllResult($v1, $v2);
$rs = array_merge($tmp, $rs);
}
}
}
// 情况2:依次运算
for($i=1; $i<4; $i++)
{
$result = [];
foreach ($data as $id=>&$v)
{
$tmp = $v;
unset($tmp[0], $tmp[1]);
$temp = getAllResult($v[0], $v[1]);
foreach($temp as $t)
{
$result[] = array_merge([$t], $tmp);
}
unset($data[$id]);
}
$data = $result;
}
$result = array_map('current', $data);
return array_merge($rs, $result);
}
/**
* 两个数的所有运算结果
* @param int $a
* @param int $b
* @return array
*/
function getAllResult($a, $b)
{
$result = [];
$result[] = "({$a}+{$b})";
$result[] = "({$a}-{$b})";
$result[] = "({$b}-{$a})";
$result[] = "({$a}*{$b})";
$result[] = "({$a}/{$b})";
$result[] = "({$b}/{$a})";
return $result;
}
/**
* 排列
* @param $arr
* @return array
*/
function permutation($arr)
{
$count = count($arr);
if($count==1)
{
return [$arr];
}
else
{
$result = [];
foreach($arr as $id=>&$v)
{
$temp = $arr;
$now = $temp[$id];
unset($temp[$id]);
$result1 = permutation($temp);
foreach($result1 as $vv)
{
array_unshift($vv, $now);
$result[] = $vv;
}
}
return $result;
}
}