如何计算最优惠的价格 [英] How to calculate the best price
问题描述
我有一个有趣的问题。我想知道一些好的方法来解决这个问题。
I have a interesting problem. I would like to know some good approaches to solve this.
我有一个小商店里,我有'N'多项产品
每个产品为非零价
与它相关联
I have a small store in which I have 'n' number of products
Each product as a non zero price
associated with it
一个产品看起来像这样
{
productId:productA;
productName:ABC;
price:20$
}
要提高客户忠诚度,我想带康宝模式进行销售。
那是,
我定义组合的m个
To enhance the customer retention, I would like to bring in combo model to sell.
That is,
I define m number of combos
每个组合看起来像这样
{
comboId:1;
comboName:2;
constituentProducts: {Product1, Product2};
comboPrice: 10$
}
每个连击告诉如果一个客户购买产品A
和产品B
,然后申请一个折扣价在组合给出定义,而不是个别价格的算术和产品A
和产品B
一个产品可以提及的两个以上的组合。
什么是计算至少价格的最佳方式
的购物篮
Each combo tells that if a customer buys productA
and productB
, then apply a discounted price given in the combo definition rather than the arithmetic sum of individual price of productA
and productB
A product may be mentioned in more than two combos.
What is the best way to calculate the least price
for a shopping basket
推荐答案
有一个HashMap的关键是产品中的连击数,其值是另一个的HashMap<产品按字母顺序排序列表,组合>。
Have a HashMap whose key is the number of products in a combo and whose value is another HashMap < Alphabetically Sorted List of Products, Combo >.
HashMap<Integer, HashMap<List<Products>, Combo>> comboMap;
现在按字母顺序排序的产品在购物车,跟踪的最低价格目前看到的和组合的是与价格相关的清单:
Now alphabetically sort the products in the shopping cart and keep track of the lowest price seen currently and the List of Combos that is associated with that price:
float lowestCurrentPrice;
List<Combo> minComboList;
有一个递归函数,随着产品组合无论是最高的号码或列表的大小启动,并试图找到在购物车与数量的产品组合。
Have a recursive function that starts with either the highest possible number of products in a combo or the size of the list, and try to find a combo in the shopping cart with that number of products.
如果找到,取出产品组合,保持组合的成本跟踪,将其添加到,现在重复使用较小的列表中递归函数的结果。
If found, remove the products in the combo, keep track of the cost of combo, and add this to the result of your recursive function that now repeats using the smaller list.
如果你已经发现了一些产品组合,比较这两个价格并保持连续技,这是你的HashMap更便宜的列表。
If you had already found a combo with that number of products, compare the two prices and keep the list of combos that was less expensive in your HashMap.
这样做,直到你用完了这个数字的连击。然后,你递减您正在寻找在组合中的项目数。如果这个数字是一,那么你只总结列表中的所有项目,否则重复使用此号码。
Do this until you run out of combos of that number. Then you decrement the number of items you are looking for in a combo. If this number is one then you just sum up all the items in the list, otherwise repeat using this number.
您应该由这个递减的结束了正确的答案,在你的第一个函数调用结束!
You should have the correct answer by the end of this decrement at the end of your first function call!
这篇关于如何计算最优惠的价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!