
Первый вариант не заработал, там break отладочный стоял

_____________
Обучаю веб-программированию качественно и не дорого: http://school-php.com
Фрилансер, принимаю заказы: PHP, JS, AS (видео-чаты). Писать в ЛС (Личные сообщения на phpforum).
Цитата |
А результат выше я выдумал.. |
Цитата |
[1341] = 1341 [1+341] = 342 [1+3+41] = 45 [1+3+4+1] = 9 [13+41] = 54 [13+4+1] = 18 [134+1] = 135 [1+34+1] = 36 |
<?php
namespace Testing\Intel\Deep;
// C //
$array = array(1,3,4,1);
$result = (int)54;
$deep = new Deep;
$deep->mathematic = array('+');
$formula = $deep->init($array,$result);
// M //
class Deep {
public $count_math;
public $i = array();
public $result;
public $mathematic = array('+','-','*','/');
public $math_var = array();
public $allactions = array();
public function init($array,$result) {
$variables = array();
$variables[] = $array;
$this->result = $result;
$this->count_math = count($this->mathematic);
$variables = array_merge($variables,$this->arrayVariables($array));
return $this->getFormula($variables,$this->mathematic);
}
public function mathVariables($deep) {
if($deep != $this->count_var) {
for($this->i[$deep]=0;$this->i[$deep]<$this->count_math;++$this->i[$deep]) {
$this->mathVariables($deep+1);
}
} else {
$line = array();
foreach($this->i as $v) {
$line[] = $v;
}
$this->math_var[] = $line;
}
}
public function arrayVariables($array) {
$variables = array();
foreach($array as $k=>$v) {
$correct = array();
if($k == (count($array)-1)) break;
if($k > 0) {
for($y=0;$y<$k;++$y) {
$correct[] = $array[$y];
}
}
for($i2=1; $i2 < count($array)-$k; ++$i2) {
$temp = array();
$temp = $correct;
$line = (string)$array[$k];
for($x=1;$x<=$i2;++$x) {
$line .= (string)$array[$k+$x];
}
$temp[] = $line;
$else = array();
while(isset($array[$k+$x])) {
$else[] = $array[$k+$x];
++$x;
}
$variables[] = array_merge($temp,$else);
if(count($else) > 1) {
$bonus = $this->arrayVariables($else);
if(count($bonus)) {
foreach($bonus as $bonus_v) {
$variables[] = array_merge($temp,$bonus_v);
}
}
}
}
}
return $variables;
}
public function getFormula($variables,$mathematic) {
$formula = array();
foreach($variables as $k=>$v) {
$deep = new Deep;
$deep->count_math = $this->count_math;
$deep->count_var = (count($v)-1);
$deep->mathVariables(0);
foreach($deep->math_var as $k2=>$v2) {
$t = '';
foreach($v as $key=>$val) {
$t .= (string)$val;
if($key != (count($v)-1)) {
$t .= $mathematic[(string)$v2[$key]];
}
}
$this->allactions[] = $t;
eval("\$sum = ".$t.";");
if($sum === $this->result) {
$t .= '='.$this->result;
$formula[] = $t;
}
}
unset($deep);
}
return $formula;
}
}
// V //
echo '<pre>'.print_r($deep->allactions,1);
echo 'Входящий массив: ['.implode(',',$array).']';
echo '<br>Поисковое число:'.$result;
if(count($formula)) {
echo '<h1>Можно. Варианты:</h1>';
foreach($formula as $v) {
echo '<div>'.$v.'</div>';
}
} else {
echo 'Нет комбинаций';
}
echo '<hr>Прими мои поздравления, супермен!';