如何将数字转换为一个价格范围 [英] How to convert a number to a range of prices

查看:128
本文介绍了如何将数字转换为一个价格范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要计算的金额给我的顾客收取,当他们买我的产品的许可证。

I want to calculate the amount to charge my customers, when they buy licenses of my product.

我的许可范围内出售:

  • 1-10:$ 50 /用户
  • 11-20:$ 40 /用户
  • 21-30:$ 30 /用户
  • 31-50:$ 20 /用户

因此​​,当有人购买136牌照,我会控告他:

So when someone purchases 136 licenses, I will charge him:

50 x 2 x $20 = $2000
30 x 1 x $30 = $900
     6 x $50 = $300

我在寻找如何处理给定的数字,并分解成发生在一个范围内的一些算法.. 我怎样才能做到这一点在纯C#或LINQ?

I'm looking for an algorithm on how to process the given number and break it into number of occurrences in a range.. How can I do this in plain C# or LINQ?

------------编辑----------------------------

------------ EDIT ----------------------------

我开始不那么令人困惑的问题(http://stackoverflow.com/questions/2685391/algorithm-for-fogbugz-pricing-scheme),我得到了我一直在寻找的答案。

I started a less confusing question (http://stackoverflow.com/questions/2685391/algorithm-for-fogbugz-pricing-scheme) and I got the answer I've been looking for.

感谢大家。

推荐答案

如果psented这个价格结构$ P $我会认为它是在客户的最佳利益通过购买最适合自己需要的软件包以最小的代价。下面的算法使用动态规划来计算的最小可能的价格准确地购买一定数量的许可证(可以节省的钱购买更多的比你需要的,虽然我还没有实现的):

If presented with this price structure I would think that it is in the customer's best interest to minimize the cost by buying the package that best suits their need. The following algorithm uses dynamic programming to calculate the minimal possible price to exactly buy a certain number of licenses (you can save money by buying more than you need, although I haven't implemented that):

int getPrice(int n)
{
    if (n >= 1 && n <= 10) return 50 * n;
    if (n >= 11 && n <= 20) return 40 * n;
    if (n >= 21 && n <= 30) return 30 * n;
    if (n >= 31 && n <= 50) return 20 * n;
    throw new Exception("Impossible");
}

int minimizePrice(int n)
{
    int[] minimumPrice = new int[n + 1];
    for (int i = 1; i <= n; ++i)
    {
        minimumPrice[i] = int.MaxValue;
        for (int j = Math.Max(0, i - 50); j < i; ++j)
        {
            minimumPrice[i] = Math.Min(minimumPrice[i],
                minimumPrice[j] + getPrice(i - j));
        }
    }
    return minimumPrice[n];
}

有关70执照的最低价格为$ 1400,可以通过购买35许可证2个街区获得。你的建议贪婪算法。这将混淆你的客户。一个聪明的客户将会把两个数量级,而不是一个大订单,节省$ 400元。

For 70 licenses the minimal price is $1400 which can be obtained by buying 2 blocks of 35 licenses. You are suggesting a greedy algorithm. This will confuse your customers. A clever customer will place two orders instead of one large order and save $400.

我建议改变你的价格,以便有没有上限的许可证的数量,你可以在每个$ 20个买了。

I'd suggest changing your prices so that there is no upper limit to the number of licenses you can buy at $20 each.

这篇关于如何将数字转换为一个价格范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆