我正在寻找一种算法,用于根据“服务器的FogBugz”定价方案(http://www.fogcreek.com/FogBugz/PriceList.html)计算购买的许可证总成本。

Fogbugz的定价为:

  • 1授权$ 299
  • 5许可证包$ 999
  • 10许可证包$ 1,899
  • 20许可证包$ 3,499
  • 50许可证包$ 7,999

  • 如果您要报价,比如说136个许可,则它们的价格为22,694美元。

    如何在C#或LINQ中做到这一点?

    任何帮助将不胜感激。

    最佳答案

    从程序员的 Angular 来看,可接受的答案虽然是一段优美的代码,却不能为客户提供最佳的价格,因此从客户的 Angular 来看,这可能不是一个优雅的解决方案。例如,当n = 4时,可接受的答案为$ 1196,但客户显然希望选择5个许可证包,而只需支付$ 999。

    可以构建一种算法,该算法可以计算出客户可以购买最低数量的许可证所需支付的最低价格。一种方法是使用动态编程。我认为这样可以解决问题:

    int calculatePrice(int n, Dictionary<int, int> prices)
    {
    
        int[] best = new int[n + prices.Keys.Max()];
        for (int i = 1; i < best.Length; ++i)
        {
            best[i] = int.MaxValue;
            foreach (int amount in prices.Keys.Where(x => x <= i))
            {
                best[i] = Math.Min(best[i],
                    best[i - amount] + prices[amount]);
            }
        }
        return best.Skip(n).Min();
    }
    
    void Run()
    {
        Dictionary<int, int> prices = new Dictionary<int, int> {
            { 1, 299 },
            { 5, 999 },
            { 10, 1899 },
            { 20, 3499 },
            { 50, 7999 }
        };
    
        Console.WriteLine(calculatePrice(136, prices));
        Console.WriteLine(calculatePrice(4, prices));
    }
    

    输出:
    22694
    999
    

    更新产生故障稍微复杂一些,但是我绝对认为这对您的客户会有所帮助。您可以执行以下操作(假设打印到控制台,尽管实际程序可能会输出到网页):
    using System;
    using System.Linq;
    using System.Collections.Generic;
    
    class Program
    {
        static Dictionary<int, int> prices = new Dictionary<int, int> {
                { 1, 299 },
                { 5, 999 },
                { 10, 1899 },
                { 20, 3499 },
                { 50, 7999 }
        };
    
        class Bundle
        {
            public int Price;
            public Dictionary<int, int> Licenses;
        }
    
        Bundle getBestBundle(int n, Dictionary<int, int> prices)
        {
            Bundle[] best = new Bundle[n + prices.Keys.Max()];
            best[0] = new Bundle
            {
                Price = 0,
                Licenses = new Dictionary<int, int>()
            };
    
            for (int i = 1; i < best.Length; ++i)
            {
                best[i] = null;
                foreach (int amount in prices.Keys.Where(x => x <= i))
                {
                    Bundle bundle = new Bundle
                    {
                         Price = best[i - amount].Price + prices[amount],
                         Licenses = new Dictionary<int,int>(best[i - amount].Licenses)
                    };
    
                    int count = 0;
                    bundle.Licenses.TryGetValue(amount, out count);
                    bundle.Licenses[amount] = count + 1;
    
                    if (best[i] == null || best[i].Price > bundle.Price)
                    {
                        best[i] = bundle;
                    }
                }
            }
            return best.Skip(n).OrderBy(x => x.Price).First();
        }
    
        void printBreakdown(Bundle bundle)
        {
            foreach (var kvp in bundle.Licenses) {
                Console.WriteLine("{0,2} * {1,2} {2,-5} @ ${3,4} = ${4,6}",
                   kvp.Value,
                    kvp.Key,
                    kvp.Key == 1 ? "user" : "users",
                    prices[kvp.Key],
                    kvp.Value * prices[kvp.Key]);
            }
    
            int totalUsers = bundle.Licenses.Sum(kvp => kvp.Key * kvp.Value);
    
            Console.WriteLine("-------------------------------");
            Console.WriteLine("{0,7} {1,-5}           ${2,6}",
                totalUsers,
                totalUsers == 1 ? "user" : "users",
                bundle.Price);
        }
    
        void Run()
        {
            Console.WriteLine("n = 136");
            Console.WriteLine();
            printBreakdown(getBestBundle(136, prices));
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("n = 4");
            Console.WriteLine();
            printBreakdown(getBestBundle(4, prices));
        }
    
        static void Main(string[] args)
        {
            new Program().Run();
        }
    }
    

    输出:
    n = 136
    
     2 * 50 users @ $7999 = $ 15998
     1 * 20 users @ $3499 = $  3499
     1 * 10 users @ $1899 = $  1899
     1 *  5 users @ $ 999 = $   999
     1 *  1 user  @ $ 299 = $   299
    -------------------------------
        136 users           $ 22694
    
    
    n = 4
    
     1 *  5 users @ $ 999 = $   999
    -------------------------------
          5 users           $   999
    

    07-28 02:42
    查看更多