【问题标题】:Discount Calculation from a Range范围内的折扣计算
【发布时间】:2012-08-02 14:57:17
【问题描述】:

折扣计算:

Product quantity and range

1  - 10    -    1%
11 - 20    -    2%
21 - 30    -    3%
31 - 40    -    4%
41 - 50    -    5%

以上是数量范围和他们给出的折扣%,

for example:
each product cost is 100
if i purchase 50 product then 5% discount is 250


Now if i purchase 50 products at 2 terms let say 20 and 30 then
for 20 product 2% discount = 40
for 30 product 3% discount = 90
total discount             = 130

但是在这里我必须得到 250 的折扣,

问题描述: 该产品可以购买n个条款的最大数量,这里最大数量为50。购买产品的折扣%是从上述范围内给出的。当添加总折扣时,它应该是相等的。在这里,当购买 50 件产品时,250 作为折扣给予相同的 250 应该是总折扣,即使产品以 20、10、10 或 25、25 的形式购买......

请帮我计算部分,用一些公式或任何东西....

【问题讨论】:

  • @tom10 伙计们,这不是家庭作业...我不是数学专家...我是一名程序员,我正在尝试在我的程序中实现它...我需要计算部分请帮助我
  • @and 我尝试了一些简单的计算,但它不起作用,我得到的总折扣要么低要么高......当我使用等于范围差异的数量术语时,我得到它,但是当数量在它变化的范围内......我不知道该怎么做......请帮助

标签: math range discount cumulative-sum


【解决方案1】:

我假设您希望折扣率始终随着购买商品数量的增加而增加,如果是这种情况,没有办法做到这一点

这是逻辑。基本方程为:

n1d1 + n2d2 + n3 d3 = (n1 + n2 + n3)dx

一个明显的解决方案是让所有的 d 都相等,即所有的贴现率都相同。否则,没有一般的解决方案(也就是说,没有一组 d 将适用于所有 n 组合 - 例如,只要除了一个 n 之外的所有 n 都为零,那么等式两边的 d 将必须相同,因此唯一的通用解决方案是所有 d 都相同),如果您想要具有不同 d 的特定解决方案,您可以在给定一组 n 的情况下求解 d 的正确值,但是当你这样做,很明显如果一个 d 小于 dx,另一个必须更大,所以你不能有一个严格增加的折扣率。

【讨论】:

    【解决方案2】:
    1. 计算上一个项目计数的折扣。 (之前给过多少折扣。)
    2. 计算新商品数量的折扣(上一个 + 当前订单)。 (客户应该有多少折扣。)
    3. 给出最终折扣作为两个值之间的差值。
    4. 将客户的(每种类型的)新项目计数存储到某个数据库中。
    float SimpleDiscount(float cost, int count)
    {
        if (count <= 0) return 0;
        if (count <= 10) return 0.01f * cost;
        if (count <= 20) return 0.02f * cost;
        if (count <= 30) return 0.03f * cost;
        if (count <= 40) return 0.04f * cost;
        return 0.05f * cost; // count > 40
    }
    
    float GetDiscount(int customerId, int itemId, int count)
    {
        float cost = GetItemCost(itemId);
    
        int previousCount = GetCustomerOrderedItemCount(customerId, itemId);
        float previousDiscount = SimpleDiscount(cost, previousCount);
    
        int newCount = previousCount + count;
        float newDiscount = SimpleDiscount(cost, newCount);
    
        SaveCustomerOrderedItemCount(customerId, itemId, newCount);
    
        return newDiscount - previousDiscount;
    }
    

    例如:

    Item cost = 100
    For 20 items: Discount = 40 (2%)
    For 30 items: Discount = 210 (7%)
    Total discount = 250 (5%)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-13
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 2016-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多