<?php
$total = 3.60; // valor total a ser testado
echo "Para um total de $total:\n\n";
$dados = array('0.10','0.20','0.30','0.50', '3.00','3.30','4.00','5.00',
'1.00','1.10','2.00','2.20');
foreach ($dados as $d)
$dadosf[] = floatval($d); // cria array paralelo em float para facilitar
for ($i = 0; $i < sizeof($dados) - 1; $i++) $base[]=array($i); // cria os primeiros valores de base
for ($n = 2;$n < sizeof($dados); $n++) { foreach ($base as $i => $b1) { // soma os números de base
$soma_base[$i]=0;
foreach ($b1 as $b2) {
$soma_base[$i] += $dadosf[$b2];
}
}
$base2 = [];
for ($b = 0; $b < sizeof($base); $b++) { $u = sizeof($base[$b])-1; // último elemento da base for ($p = $base[$b][$u] + 1; $p < sizeof($dados); $p++) { echo 'Combinação ' . ++$c . ': ';
for ($d = 0; $d <= $u; $d++) {
echo $dados[$base[$b][$d]] . ' + ';
}
echo $dados[$p] . ' = ' . $total . "\n";
}
$base2[] = $base[$b];
$base2[sizeof($base2)-1][] = $p;
}
}
$base = $base2;
}
PD9waHAKJHRvdGFsID0gMy42MDsgLy8gdmFsb3IgdG90YWwgYSBzZXIgdGVzdGFkbwplY2hvICJQYXJhIHVtIHRvdGFsIGRlICR0b3RhbDpcblxuIjsKJGRhZG9zID0gYXJyYXkoJzAuMTAnLCcwLjIwJywnMC4zMCcsJzAuNTAnLAogICAgICAgICAgICAgICAnMy4wMCcsJzMuMzAnLCc0LjAwJywnNS4wMCcsCiAgICAgICAgICAgICAgICcxLjAwJywnMS4xMCcsJzIuMDAnLCcyLjIwJyk7CmZvcmVhY2ggKCRkYWRvcyBhcyAkZCkgCgkkZGFkb3NmW10gPSBmbG9hdHZhbCgkZCk7IC8vIGNyaWEgYXJyYXkgcGFyYWxlbG8gZW0gZmxvYXQgcGFyYSBmYWNpbGl0YXIKCmZvciAoJGkgPSAwOyAkaSA8IHNpemVvZigkZGFkb3MpIC0gMTsgJGkrKykgCgkkYmFzZVtdPWFycmF5KCRpKTsgLy8gY3JpYSBvcyBwcmltZWlyb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