`

根据总用量计算每种包装规格的购买量和总价 后续篇(一)并且使得用户花费最少

 
阅读更多

  在根据总用量计算每种包装规格的购买量和总价 中讲述了一个开发的场景,首先有一个总共的土地拥有量:total亩,现在有n种可以选择的商品,每一种商品都有每亩的用量:amount/亩,每一种商品都有多个包装规格,每个包装规格有自己的包装量和售价。

  需求就是计算出,在给定土地上面种植那种商品需要的花费最少,也就是可以用最少的钱来买更多的东西种地,满足我种地的最大需求量。

  其实我们还假设了一个前提,就是包装大的单价低,好比100克每包的可能买100元,200克每包的就应该卖180元,肯定会小于两个100克每包的价格,这个也是市场调研的结果,也比较符合市场常规。

  代码中用到了二分搜索法的思路,其实我需要的是一个定位,定位我需要购买的最合算的包装规格,也就是在满足需要的情况下,尽可能购买大包装。

  例如:现在有两个商品,土豆和白菜,土豆有三个规格,100克每包的100元,200克每包的180元,300克每包的240元;白菜有四个规格,100克每包的70元,200克每包的130元,300克每包的200元,400克每包的300元。种白菜每亩要100斤,中土豆每亩尧200斤。我总共有1029亩地,计算一下,是种土豆花费少?还是种白菜花费少?

  有两个点要注意:1)找位置,找到我需要购买的最合算的包装规格的位置,使用了二分搜索2)计算用量和预算。

  计算一种商品最少花费的伪代码如下:

  输入:一个商品的信息,包括包装规格

  输出:需要购买的包装规格几个数,总共的花费

  计算过程:

  {

    while(总量>最小包装规格的量)

    {

      1)找位置,定位包装规格

      2)计算需要购买多少个第一步找到的包装规格

      3)需要购买的个数=总量和包装规格的包装量取整

      4)总量=总量-需要购买的个数*包装规格的包装量

      5)这种商品的预算+=需要购买的个数*包装规格的单价

    }

    if(总量<=最小包装规格的量)

    {

      说明零头就只能买个最小包装了,因为购买不支持散买。

    }

  }

 

  示例代码下载:/Files/virusswb/BeautyCode.ConApp.rar

  代码如下:

 

代码
<!--<br/ /><br/ />Code highlighting produced by Actipro CodeHighlighter (freeware)<br/ />http://www.CodeHighlighter.com/<br/ /><br/ />-->/*
 * Created by SharpDevelop.
 * User: haier
 * Date: 2010-3-23
 * Time: 22:19
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/
using System;
using System.Collections.Generic;

namespace BeautyCode.ConApp
{
    
/// <summary>
    
/// 种植方案
    
/// </summary>
    public class PlantArea
    {
        
/// <summary>
        
/// 种植面积,单位是亩
        
/// </summary>
        public static decimal PlantAreaAmount=175.25m;
    }
    
/// <summary>
    
/// 商品类型
    
/// </summary>
    public enum ProductType
    {
        
/// <summary>
        
/// 种子
        
/// </summary>
        Seed,
        
/// <summary>
        
/// 肥料
        
/// </summary>
        Fertilizer,
        
/// <summary>
        
/// 农药
        
/// </summary>
        Pesticide
    }
    
    
public enum FertType
    {
        
/// <summary>
        
/// 底肥
        
/// </summary>
        DiF,
        
/// <summary>
        
/// 种肥
        
/// </summary>
        ZhongF,
        
/// <summary>
        
/// 追肥
        
/// </summary>
        ZhuiF
    }
    
public class Unit
    {
        
public Guid ID{set;get;}
        
        
public string CnName{set;get;}
    }
    
public class PkgPrice
    {
        
public Guid PkgID{set;get;}
        
/// <summary>
        
/// 包装量
        
/// </summary>
        public decimal PkgAmount{set;get;}
        
/// <summary>
        
/// 包装单位
        
/// </summary>
        public Unit PkgUnit{set;get;}
        
/// <summary>
        
/// 包装价格
        
/// </summary>
        public decimal Price{set;get;}
        
/// <summary>
        
/// 选中的包装个数
        
/// </summary>
        public int Quantities{set;get;}
    }
    
    
public class Product
    {
        
public virtual Guid ID{set;get;}
        
public virtual string Name{set;get;}
        
public virtual ProductType ProType{set;get;}
        
/// <summary>
        
/// 每亩用量
        
/// </summary>
        public virtual decimal Amount{set;get;}
        
/// <summary>
        
/// 每亩用量单位
        
/// </summary>
        public virtual Unit ProductUnit{set;get;}
        
/// <summary>
        
/// 总用量
        
/// </summary>
        public virtual decimal TotalAmount{set;get;}
        
public Product (ProductType type)
        {
            
this.ProType=type;
        }
        
/// <summary>
        
/// 预算
        
/// </summary>
        public decimal Budget{set;get;}
        
/// <summary>
        
/// 全部包装类型
        
/// </summary>
        public List<PkgPrice > AllPkgPrice{set;get;}
        
/// <summary>
        
/// 选中的包装类型
        
/// </summary>
        public List<PkgPrice > SelectPkgPrice{set;get;}
    }
    
public class Seed:Product 
    {
        
public Seed ():base(ProductType.Seed ){}
        
        
public List<Pesticide > Pesticides{set;get;}
        
public List<DiF > DiFs{set;get;}
        
public List<ZhongF >ZhongFs{set;get;}
        
public List<ZhuiF >ZhuiFs{set;get;}
    }
    
public class Fertilizer:Product 
    {
public FertType FType{set;get;}
        
public Fertilizer (FertType type):base (ProductType.Fertilizer){
        
this.FType=type ;
        }
    }
    
public class DiF:Fertilizer 
    {
        
public DiF ():base (FertType.DiF ){}
    }
    
public class ZhongF:Fertilizer 
    {
        
public ZhongF ():base (FertType .ZhongF ){}
    }
    
public class ZhuiF:Fertilizer 
    {
        
public ZhuiF ():base (FertType.ZhuiF ){}
    }
    
public class Pesticide:Product 
    {
        
public Pesticide ():base(ProductType.Pesticide ){}
    }
    
/// <summary>
    
/// Description of BeiBao.
    
/// </summary>
    public class BeiBao
    {
        
public List<Seed  >Seeds=null;
        
public Seed  Seed=null;
        
public List<Pesticide >Pesticides=null;
        
public Pesticide Pesticide=null;
        
public List<DiF >DiFs=null;
        
public DiF DiF=null;
        
public List<ZhongF >ZhongFs=null;
        
public ZhongF ZhongF=null;
        
public List<ZhuiF >ZhuiFs=null;
        
public ZhuiF ZhuiF=null;
        
public List<PkgPrice > PkgPrices=null;
        
public BeiBao()
        {
            Seeds 
=new List<Seed  >();
            Seed 
=new Seed(){ ID=Guid.NewGuid (),
                Name
="土豆", Amount =120, Budget=0,
                TotalAmount 
=120*PlantArea.PlantAreaAmount };
            PkgPrices 
=new List<PkgPrice>(){
                
new PkgPrice (){ PkgID=Guid.NewGuid (),PkgAmount =100, Price=23},
                
new PkgPrice (){ PkgID=Guid.NewGuid (),PkgAmount =210, Price=58},
                
new PkgPrice (){ PkgID=Guid.NewGuid (),PkgAmount =130, Price=39}};
            Seed .AllPkgPrice 
=PkgPrices ;
            Seed.AllPkgPrice.Sort(
new Comparison<PkgPrice >(comparePkgAmountAsc));
            Seeds.Add(Seed );
            Seed 
=new Seed(){ ID=Guid.NewGuid (),
                Name
="白菜", Amount =150, Budget=0, TotalAmount =150*PlantArea.PlantAreaAmount };
            PkgPrices 
=new List<PkgPrice>(){
                
new PkgPrice (){ PkgID=Guid.NewGuid (),PkgAmount =80, Price=20},
                
new PkgPrice (){ PkgID=Guid.NewGuid (),PkgAmount =180, Price=48},
                
new PkgPrice (){ PkgID=Guid.NewGuid (),PkgAmount =110, Price=34}};
            Seed .AllPkgPrice 
=PkgPrices ;
                Seed.AllPkgPrice.Sort(
new Comparison<PkgPrice >(comparePkgAmountAsc));
            Seeds.Add(Seed );
            Seed 
=new Seed(){ ID=Guid.NewGuid (),
                Name
="萝卜", Amount =100, Budget=0, TotalAmount =100*PlantArea.PlantAreaAmount };
            PkgPrices 
=new List<PkgPrice>(){
                
new PkgPrice (){ PkgID=Guid.NewGuid (),PkgAmount =110, Price=35},
                
new PkgPrice (){ PkgID=Guid.NewGuid (),PkgAmount =250, Price=79},
                
new PkgPrice (){ PkgID=Guid.NewGuid (),PkgAmount =170, Price=49}};
            Seed .AllPkgPrice 
=PkgPrices ;
                Seed.AllPkgPrice.Sort(
new Comparison<PkgPrice >(comparePkgAmountAsc));
            Seeds.Add(Seed );
            Seed 
=new Seed(){ ID=Guid.NewGuid (),
                Name
="大豆", Amount =20, Budget=0, TotalAmount =20*PlantArea.PlantAreaAmount };
            PkgPrices 
=new List<PkgPrice>(){
                
new PkgPrice (){ PkgID=Guid.NewGuid (),PkgAmount =110, Price=35},
                
new PkgPrice (){ PkgID=Guid.NewGuid (),PkgAmount =250, Price=79},
                
new PkgPrice (){ PkgID=Guid.NewGuid (),PkgAmount =170, Price=49}};
            Seed .AllPkgPrice 
=PkgPrices ;
                Seed.AllPkgPrice.Sort(
new Comparison<PkgPrice >(comparePkgAmountAsc));
            Seeds.Add(Seed );
        }
        
        
public Product ComputeOptimized()
        {
            
            
foreach (Product p in this.Seeds  )
            {
                GetTotalBudget ( p);
            }
            Seeds.Sort (
new Comparison<Seed  >(compareProductBudgetAsc ));
            
return Seeds[0];
        }
        
private void  GetTotalBudget(Product p)
        {
            p.SelectPkgPrice
=new List<PkgPrice>();
            
int index=-1;
            
int quantities=0;
            
decimal totalamount=p.TotalAmount;
            
while (totalamount >p.AllPkgPrice [0].PkgAmount )
            {
                index 
=binaryLocalize (p.AllPkgPrice,totalamount,p.AllPkgPrice.Count );
                
if(index !=-1)
                {
                    quantities 
=(int)(totalamount /p.AllPkgPrice [index ].PkgAmount );
                    
//quantities =(int)(totalamount %p.AllPkgPrice [index ].PkgAmount );
                    p.SelectPkgPrice.Add(new PkgPrice (){
                                             PkgID
=p.AllPkgPrice[index].PkgID ,
                                             PkgAmount
=p.AllPkgPrice[index].PkgAmount,
                                             Price
=p.AllPkgPrice[index].Price ,
                                             Quantities
=quantities 
                                         });
                    p.Budget 
+=quantities *p.AllPkgPrice[index].Price ;
                }
                totalamount 
-=quantities *p.AllPkgPrice[index].PkgAmount;
            }
            
if(totalamount !=0)
            {
                quantities 
=1;
                index 
=0;
                p.SelectPkgPrice.Add(
new PkgPrice (){
                                             PkgID
=p.AllPkgPrice[index].PkgID ,
                                             PkgAmount
=p.AllPkgPrice[index].PkgAmount,
                                             Price
=p.AllPkgPrice[index].Price ,
                                             Quantities
=quantities 
                                         });
                    p.Budget 
+=quantities *p.AllPkgPrice[index].Price ;
            }
            
        }
        
/// <summary>
        
/// 二分法搜索元素
        
/// </summary>
        
/// <param name="a"></param>
        
/// <param name="x"></param>
        
/// <param name="n"></param>
        
/// <returns></returns>
        private int binarySearch(List<int >a,int x,int n)
        {
            
int left=0;
            
int right=n-1;
            
while (left <=right)
            {
                
int middle=(left +right )/2;
                
if(x==a[middle])return middle;
                
if(x>a[middle ])left =middle +1;
                
else right =middle -1;
            }
            
//没有找到
            return -1;
        }
        
/// <summary>
        
/// 二分法定位规格
        
/// </summary>
        
/// <param name="pkgPrices"></param>
        
/// <param name="totalAmount"></param>
        
/// <param name="n"></param>
        
/// <returns></returns>
        private int binaryLocalize(List<PkgPrice > pkgPrices,decimal  totalAmount,int n)
        {
            
if(totalAmount >=pkgPrices [n-1].PkgAmount )
                
return n-1;
            
            
int left=0;
            
int right=n-1;
            
while (left <=right )
            {
                
int middle=(left+right )/2;
                
if(totalAmount >=pkgPrices [middle ].PkgAmount &&totalAmount <pkgPrices [middle +1].PkgAmount )
                {
                    
return middle ;
                }
                 
if(totalAmount ==pkgPrices [middle +1].PkgAmount )
                {
                    
return middle +1;
                }
                 
if(totalAmount >pkgPrices [middle +1].PkgAmount )
                     left 
=middle +1;
                 
else
                     right 
=middle -1;
            }
            
return -1;
        }
        
private int compareProductBudgetAsc(Product pro1,Product pro2)
        {
            
if(pro1.Budget>pro2 .Budget )
                
return 1;
            
if(pro1.Budget<pro2 .Budget )
                
return -1;
            
else 
                
return 0;
        }
        
        
private int comparePkgAmountAsc(PkgPrice pkg1,PkgPrice pkg2)
        {
            
if(pkg1.PkgAmount >pkg2.PkgAmount )
                
return 1;
            
if(pkg1.PkgAmount <pkg2 .PkgAmount )
                
return -1
分享到:
评论

相关推荐

    SQL语句在审计

    SQL语句在审计的简单应用

    疏浚吹填工程量计算程序

    一款三角网法计算疏浚工程及吹填区吹填工程量计算软件,计算速度很快。

    Java Web应用开发 35 课堂案例-调用存储过程统计商品总价.docx

    Java Web应用开发 35 课堂案例-调用存储过程统计商品总价.docx 学习资料 复习资料 教学资源

    js七折五折八折计算价格

    Javascript根据竞拍价格、数量和选择的支付方式计算总价。1. JavaScript的窗体控制 2. DHTML DOM的Form对象 3. DHTML DOM的Input对象 4. DHTML DOM的Select和Option对象 5. DHTML DOM的Textarea对象 6. JavaScript的...

    四舍五入_四舍五入问题_源码

    输入苹果的单价及购买的数量,计算总价,分别显示总价的总价整数部分和四舍五入部后的整数部分

    ease软件 4.0版的特点和应用

    用户可以自己规定一个时间作为早期反射声的界限,ease4.0会在standardmapping和render mapping中计算这个反射声。ease4.0中的局部声线跟踪法允许用户在任意一个位置发出一条声线,用来计算局部衰变时间。 ease3.0中...

    软件设计模式作业 结构型设计模式

    用组合模式实现当用户在商店购物后,显示其所选商品信息,并计算所选商品总价的功能。 说明:假如王同学到万达生活用品店购物。 5、享元模式 5.1 作业题目 在天猫商城里存在着成千上万的网店,但是天猫所提供的网站...

    快速订购与显示购买量与浏览数

    快速订购与显示购买量与浏览数

    Shopping:计算购物篮的总价。 使用 Java 8 的函数式响应式解决方案

    使用您喜欢的任何语言,编写一个简单的程序来计算一篮子购物的价格。 项目在列表中一次显示一个,按名称标识 - 例如“Apple”或“Banana”。 多个项目在列表中多次出现,例如 ["Apple", "Apple", "Banana"] 是一个...

    java源码之计算机配件报价系统项目的实现.zip

    报价功能:用户可以根据自己的需求,在系统中选择需要的配件,并生成相应的报价单,系统会根据用户选择的配件自动计算总价格,并提供报价单的下载和打印功能。 配件比较:系统提供了配件参数和价格的比较功能,用户...

    简单的购物车实现计算

    jsp的session实现购物车功能,显示商品,购买商品,结算,在MYELIPSE导入即可运行。

    couName.js

    世界地图的中英文对照,以及对每个国家按字典序的编号,以字典形式存储,方便由键取值,可用于ECahrts。

    数据运营思维导图

    看贴功能内浏览了3篇贴子的新用户和仅浏览1篇贴子的新用户进行分析 来自A渠道的新用户进行(有使用看贴/未使用看贴)行为分组比较 渠道对比 是不是某些渠道的量出现问题 用户行为 功能使用及参与度 页面访问...

    2019数据运营思维导图

    解决问题 了解产品的付费用户规模 付费用户整体的稳定性 了解付费用户构成 鲸鱼用户、海豚用户、小鱼用户各自数量和比例 注意事项 数据是去重的 ARPU 名词定义 平均每活跃用户收入 统计时间段内,总收入/活跃用户数...

    用c++实现的贪心算法具体源代码

    第一行是由空格分隔的两个正整数n和k,分别表示有n种商品和明明购买礼物总重量,第二行为空格分隔的n个正整数,表示现有每种商品的数量,第三行为空格分隔的n个正整数,对应第二行每种商品的总价。 输出说明 一个...

    设计模式之装饰模式:以售卖咖啡为例

    根据售卖咖啡的过程,客户先选择一种口味的咖啡,又选择不同种类的配料,由于配料选择是不定的,符合装饰模式的应用情况。

    1104 计算书费.cpp

    给定每种图书购买的数量,编程计算应付的总费用。 【输入】 输入一行,包含10个整数(大于等于0,小于等于100),分别表示购买的《计算概论》、《数据结构与算法》、《数字逻辑》、《C++程序设计教程》、《人工智能》...

    DVD在线租赁优化方案

    先根据给出的1000位会员对每种DVD的满意度求出每种DVD 的需求人数,利用第一问虽小购买量与会员 需求量成正比关系的结论,运用计算机模拟的方法,先确定一较小的购买量,再用贪婪算法作为策略进行分 配,计算满意的...

    javaweb开发:通过对jsp和servlet的学习做一个简单的购物车

    用jsp编写三个页面,一个显示一些历史图书的名称和价格, 一个显示 一些计算机图书的名称和价格 ,另一个 显示为外语书的名称和价格。每本书后面 都有一个链接:购买。单击链接,能够将该本书加到 购物车。每个...

    银行自动取款机ATM系统需求分析

    拟开发一个自动取款系统(参考图1-1),它是一个由自动取款机、中央计算机、分行计算机及柜员终端组成的网络系统。ATM和中央计算机由总行投资购买。总行拥有多台ATM,分别设在全市各主要街道上。分行负责提供分行...

Global site tag (gtag.js) - Google Analytics