PHP Developer Forum Hier habt ihr die Möglichkeit, eure Skriptprobleme mit anderen Anwendern zu diskutieren. Seid so fair und beantwortet auch Fragen von anderen Anwendern. Dieses Forum ist sowohl für ANFÄNGER als auch für PHP-Profis! Fragen zu Laravel, YII oder anderen PHP-Frameworks. |
 |

06-02-2017, 14:47
|
sanktusm
Registrierter Benutzer
|
|
Registriert seit: Aug 2005
Beiträge: 660
|
|
Beste Kombination ermitteln
Hallo,
ich habe hier ein Script mit dem ich die nächstliegende Kombination von Bauplatten der Länge 2.5 und 4 ermitteln möchte:
Mir erscheint die Lösung nah, indem ich in 0.5er Schritten die Länge ermittle, die passt:
PHP-Code:
<?php $totals = array(); $x=0;
function getAllCombinations($ind, $denom, $n, $vals=array()){ global $totals, $x; if ($n == 0){ foreach ($vals as $key => $qty){ for(; $qty>0; $qty--){ $totals[$x][] = $denom[$key]; } } $x++; return; } if ($ind == count($denom)) return; $currdenom = $denom[$ind]; for ($i=0;$i<=($n/$currdenom);$i++){ $vals[$ind] = $i; getAllCombinations($ind+1,$denom,$n-($i*$currdenom),$vals); } }
function ceiling($number, $significance = 1) { return ( is_numeric($number) && is_numeric($significance) ) ? (ceil($number/$significance)*$significance) : false; }
$array = array(2.5, 4); $sum = 0.5; $length = ceiling(11.4, 0.5);
while(empty(getAllCombinations(0, $array, $length))) {
$length = $length + 0.5; }
getAllCombinations(0, $array, $sum); echo "<pre>"; print_r($totals); echo "</pre>";
Leider gibt die function den Wert total nicht zurück. Weiß jemand wie ich das hinkriege?
Geändert von sanktusm (06-02-2017 um 15:04 Uhr)
|

06-02-2017, 16:18
|
sanktusm
Registrierter Benutzer
|
|
Registriert seit: Aug 2005
Beiträge: 660
|
|
günstigste Kombination von Längen finden:
Wenn man eine gegebene Länge (mit kleinen Überlappungen) mit 2 Platten der Länge 4 und 2.5 auskleiden will, bekommt man mit dieser Funktion eine Lösung:
PHP-Code:
<?php $totals = array(); $x=0;
function getAllCombinations($ind, $denom, $n, $vals=array()){ global $totals, $x; if ($n == 0){ foreach ($vals as $key => $qty){ for(; $qty>0; $qty--){ $totals[$x][] = $denom[$key]; } } $x++; return; } if ($ind == count($denom)) return; $currdenom = $denom[$ind]; for ($i=0;$i<=($n/$currdenom);$i++){ $vals[$ind] = $i; getAllCombinations($ind+1,$denom,$n-($i*$currdenom),$vals); } return $totals; }
function ceiling($number, $significance = 1) { return ( is_numeric($number) && is_numeric($significance) ) ? (ceil($number/$significance)*$significance) : false; }
$array = array(2.5, 3, 4); $length = ceiling(16.4, 0.5);
$totals = array(); $x=0;
while(empty(getAllCombinations(0, $array, $length))) {
$length = $length + 0.5; }
$smallest = Array();
$totals = array(); $x=0;
$total_arr = getAllCombinations(0, $array, $length);
foreach($total_arr as $combinations) {
$smallest[] = count($combinations);
}
$min_val = 0; $min_key = 0; foreach($smallest as $key => $val) { if ($val < $min_val) { $min_val = $min; $min_key = $key; } }
$numberofoverlays = count($total_arr[$min_key]) - 1;
$length = $length + ($numberofoverlays * 0.3);
$length = ceiling($length, 0.5);
$totals = array(); $x=0;
while(empty(getAllCombinations(0, $array, $length))) {
$length = $length + 0.5; } $totals = array(); $x=0;
$total_arr = getAllCombinations(0, $array, $length);
echo "<pre>"; print_r($total_arr[$min_key]); echo "</pre>";
|
Aktive Benutzer in diesem Thema: 1 (Registrierte Benutzer: 0, Gäste: 1)
|
|
Themen-Optionen |
|
Thema bewerten |
|
Forumregeln
|
Es ist Ihnen nicht erlaubt, neue Themen zu verfassen.
Es ist Ihnen nicht erlaubt, auf Beiträge zu antworten.
Es ist Ihnen nicht erlaubt, Anhänge hochzuladen.
Es ist Ihnen nicht erlaubt, Ihre Beiträge zu bearbeiten.
HTML-Code ist aus.
|
|
|
|
PHP News
|