为了账号安全,请及时绑定邮箱和手机立即绑定

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 关注
  • 57 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信