首页 新闻 会员 周边 捐助

求解购买不同产品时计算最少运费的算法

0
悬赏园豆:50 [待解决问题]
已知有四个不同的产品:
A001,重量为100g,支持的递送方式为:免运费平邮、EMS、DHL、FedEx;
A002,重量为200g,支持的递送方式为:EMS、DHL、UPS;
A003,重量为150g,支持的递送方式为:DHL、UPS;
A004,重量为500g,支持的递送方式为:免运费平邮、FedEx、UPS。
 
快递收费标准如下:
免运费平邮:300g以内免费,超过300g(该重量按订单总重来算),则每100g收费5元快递费;
EMS:每100g收费10元快递费,不足100g按100g计算;
DHL:每200g收费20元快递费,不足200g按200g计算;
UPS:每300g收费25元快递费,不足300g按300g计算;
FedEx:每200g收费20元快递费,不足200g按200g计算;
 
请编写程序,计算这几个产品同时购买时,最少收费,且最少分包的组合(注意,没有共同的递送方式时,不能在一个分包组合里面),且给出最终的计算结果。
 
求解方法……
 
为了大家不需要打那么多代码,此处特附上基础代码……
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsolePrj
{
    class ExpressCalc
    {
        public void Test()
        {
            List<Product> lstProduct = new List<Product>();
            #region 处理产品表
            lstProduct.Add(new Product("A001", 100)
                    {
                        ExpressList = new List<IExpress>()
                    {
                        new NormalExpress(),
                        new EMS(),
                        new DHL(),
                        new FedEx()
                    }
                    }
                );

            lstProduct.Add(new Product("A002", 200)
            {
                ExpressList = new List<IExpress>()
                    {
                        new EMS(),
                        new DHL(),
                        new UPS()
                    }
            }
            );

            lstProduct.Add(new Product("A003", 100)
            {
                ExpressList = new List<IExpress>()
                    {
                        new DHL(),
                        new UPS()
                    }
            }
            );

            lstProduct.Add(new Product("A004", 100)
            {
                ExpressList = new List<IExpress>()
                    {
                        new NormalExpress(),
                        new UPS(),
                        new FedEx()
                    }
            }
            ); 
            #endregion

            //求解算法……
        }
    }

    #region 快递方式
    public abstract class IExpress
    {
        public IExpress(string name)
        {
            this.Name = name;
        }

        public string Name
        {
            get;
            set;
        }

        public abstract double GetPrice(double weight);
    }

    public class NormalExpress : IExpress
    {
        public NormalExpress()
            : base("免运费平邮")
        {
        }
        public override double GetPrice(double weight)
        {
            if (weight <= 300)
            {
                return 0;
            }
            else
            {
                double p = weight - 300;
                if (p % 300 == 0)
                {
                    return (p / 300) * 5;
                }
                else
                {
                    return (p / 300 + 1) * 5;
                }
            }
        }
    }

    public class EMS : IExpress
    {
        public EMS()
            : base("EMS")
        {
        }
        public override double GetPrice(double weight)
        {
            double unitWeight = 100;
            double everyMoney = 10;
            if (weight % unitWeight == 0)
            {
                return (weight / unitWeight) * everyMoney;
            }
            else
            {
                return (weight / unitWeight + 1) * everyMoney;
            }
        }
    }

    public class DHL : IExpress
    {
        public DHL()
            : base("DHL")
        {
        }
        public override double GetPrice(double weight)
        {
            double unitWeight = 200;
            double everyMoney = 20;
            if (weight % unitWeight == 0)
            {
                return (weight / unitWeight) * everyMoney;
            }
            else
            {
                return (weight / unitWeight + 1) * everyMoney;
            }
        }
    }

    public class UPS : IExpress
    {
        public UPS()
            : base("UPS")
        {
        }
        public override double GetPrice(double weight)
        {
            double unitWeight = 300;
            double everyMoney = 25;
            if (weight % unitWeight == 0)
            {
                return (weight / unitWeight) * everyMoney;
            }
            else
            {
                return (weight / unitWeight + 1) * everyMoney;
            }
        }
    }

    public class FedEx : IExpress
    {
        public FedEx()
            : base("FedEx")
        {
        }
        public override double GetPrice(double weight)
        {
            double unitWeight = 200;
            double everyMoney = 20;
            if (weight % unitWeight == 0)
            {
                return (weight / unitWeight) * everyMoney;
            }
            else
            {
                return (weight / unitWeight + 1) * everyMoney;
            }
        }
    } 
    #endregion

    public class Product
    {
        public Product(string name, double weight)
        {
            this.Name = name;
            this.Weight = weight;
        }

        public string Name
        {
            get;
            set;
        }

        public double Weight
        {
            get;
            set;
        }

        public List<IExpress> ExpressList
        {
            get;
            set;
        }
    }
}

 

qfzhang的主页 qfzhang | 初学一级 | 园豆:156
提问于:2012-10-14 21:02
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册