亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

PHP輸出不同數字組合相加的值

PHP輸出不同數字組合相加的值

PHP
瀟湘沐 2023-08-11 17:06:47
我有一個可以包含一系列附加組件的產品,我需要計算包含任意數量附加組件的產品的價格。目前有五個附加組件,但將來可能會有更多。一開始只有幾個,我只是硬編碼不同的組合,但現在我已經有了五個,這種方法變得不可行。產品的基本價格可能為 10 美元,附加產品的價格可能不同,例如 10 美元 + A + B、10 美元 + A + B + C、10 美元 + A + C + D + E 等等。每個產品還需要 3x、6x 和 12x 的乘數,因此之前的組合,但如果產品的基本價格為 10 美元,則添加的數字為 30 美元、60 美元或 120 美元。理想情況下,我該如何在 PHP 中做到這一點并刪除重復值?
查看完整描述

1 回答

?
手掌心

TA貢獻1942條經驗 獲得超3個贊

不要將每個附加組件視為特殊情況,而是一般地識別每個附加組件。如果您使用面向對象的結構,這可能看起來像一個AddOn接口或基類,其中有一個方法返回其成本和所需的任何其他屬性。然后,您只需從基本價格開始,然后循環遍歷乘數(如果沒有乘數,則為 1)。該解決方案的關鍵部分是使用遞歸和迭代的組合來確定附加組件的所有可能的排列。


像這樣的東西:


class AddOn{


    private $price, $description;


    public function __construct(float $price, string $description){

        $this->price=$price;

        $this->description=$description;

    }


    public function getPrice(): float{

        return $this->price;

    }


    public function getDescription(): string{

        return $this->description;

    }


}


class ProductConfiguration{


    private $basePrice, $multiplier, $addOns;


    public function __construct(float $basePrice, int $multiplier, array $addOns){

         $this->basePrice=$basePrice;

         $this->multiplier=$multiplier;

         $this->addOns=$addOns;

    }


    public function getPrice(): float{

        $price=$this->basePrice*$this->multiplier;

        foreach($this->addOns as $addOn)

            $price+=$addOn->getPrice();

        return $price;

    }


    public function getMultiplier(): int{

        return $this->multiplier;

    }


    public function getAddOns(): array{

        return $this->addOns;

    }


}


$basePrice=10;


$addOns=[

    new AddOn(5, "AddOn A"),

    new AddOn(1, "AddOn B"),

    new AddOn(20, "AddOn C")

];

$permutations=[[]]; //Include an empty set as a possible option

//This recursive function accepts the base set of add-ons, a reference to an array to which to add any permutations, and a base parameter that will be used only internally to pass the parent permutations

function getPermutations(array $addOns, array &$permutations, array $base=[]): void{

    //array_unshift removes the first item from the array, since this is done first, it will prevent duplicate combinations that differ only in order

    while(($current=array_shift($addOns))!==null){

        //Combine the parent permutation($base) with the next value in the array to create this permutation

        $permutation=array_merge($base, [$current]);

        $permutations[]=$permutation; //Push to the referenced array

        getPermutations($addOns, $permutations, $permutation); //Recursively compute all permutations that begin with the current one

    } //Subsequent iterations of the while loop will handle each value in the array as the initial value without each other value

}

getPermutations($addOns, $permutations);


$multipliers=[

    1,

    3,

    6,

    12

];

$configurations=[];

foreach($multipliers as $m){

    foreach($permutations as $p){

       $configurations[]=new ProductConfiguration($basePrice, $m, $p);

    }

}

//$configurations now contains the set of (de-duplicated) product configurations(each possible combination)



查看完整回答
反對 回復 2023-08-11
  • 1 回答
  • 0 關注
  • 115 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號