select the best breakdown of a set of products

Asked 2 years ago, Updated 2 years ago, 405 views

I have a request to purchase the product from a customer.
I'd like to change that product to the best set of products and buy it at the lowest price.
Please let me know if you have a good algorithm.

Example)
Customer Purchase Request
 Item A 20 unit price 200 yen Total price 4000 yen
 Item B Unit price 300 yen Total price 3000 yen

Set item
 Product AB Set Price 450 Yen
Breakdown
   Product A 1 ea
   Item B 1p

In the above case, it is best for the customer to purchase 10 units of product AB set and 10 units of product A as a single item.
Please let me know if there is a good algorithm to choose the best combination like this.
It does not matter what language the program is in.
Thank you for your cooperation.

algorithm

2022-10-23 16:01

2 Answers

If I'm right,

Always:

Product A (200 yen) + Product B (300 yen) > Product AB set (450 yen)

So the solution is:

Xa=Product A amount
Xb = Product B amount

if Xa>Xb
then

    Purchase ← (Xa-Xb) Product A
    Purchase ← (Xb) Product AB Set

else

    Purchase ← (Xb-Xa) Product B
    Purchase ← (Xa) Product AB Set

end


2022-10-23 16:01

Wouldn't it be best to use dynamic planning for 0-1 knapsack problems?


2022-10-23 16:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.