Pro zajímavost zde je kód který používám
class Variation {
var $pieces_arr=array();
var $prices_arr=array();
var $sum_count=0;
var $sum_price=0;
function Variation() {
}
function setPieces($type, $count, $price){
$this->pieces_arr[$type] = $count;
$this->prices_arr[$type] = $count*$price;
$this->sum_count = array_sum($this->pieces_arr);
$this->sum_price = array_sum($this->prices_arr);
}
}
class VariationSolver {
var $itemsArr = array();
var $reqCount = 0;
var $BestVar;
function VariationSolver() {
$this->BestVar = null;
}
function setRequestedCount($count){
$this->reqCount = $count;
}
function addVariationItem($type, $piece, $price){
$this->itemsArr[] = array($type, $piece, $price);
}
function compute($level, $VarObj){
$max_level = sizeof($this->itemsArr)-1;
$increment = $this->itemsArr[$level][1];
for ($i = 0; $i==0 || ($i <= $this->reqCount && ($VarObj->sum_count+$increment)<=$this->reqCount) ; $i=$i+$increment) {
$VarObj->setPieces($this->itemsArr[$level][0], $i, $this->itemsArr[$level][2]);
if ($max_level>$level){
$this->compute($level+1, $VarObj);
}
else {
if ($VarObj->sum_count==$this->reqCount){
if ($this->BestVar == null || $VarObj->sum_price < $this->BestVar->sum_price){
$this->BestVar = $VarObj;
}
}
}
}
}
function getBestVar(){ //slow
$this->BestVar = null;
if (sizeof($this->itemsArr)>0){
$this->compute(0, new Variation());
}
return $this->BestVar;
}
}
//==== Vypocet ====
$VS = new VariationSolver();
$VS->setRequestedCount(100);
$VS->addVariationItem("N",1,100); // typ baleni, pocet kusu v baleni, cena za kus
$VS->addVariationItem("B",2,95); // typ baleni, pocet kusu v baleni, cena za kus
$VS->addVariationItem("A1",5,90); // typ baleni, pocet kusu v baleni, cena za kus
$VS->addVariationItem("D",10,98); // typ baleni, pocet kusu v baleni, cena za kus
$VS->addVariationItem("IC1",20,81); // typ baleni, pocet kusu v baleni, cena za kus
$VS->addVariationItem("IC2",50,80); // typ baleni, pocet kusu v baleni, cena za kus
print "<pre>";
print_r( $VS->getBestVar() );
print "</pre>";