- 浏览: 475572 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (1028)
- [发布至博客园首页] (826)
- [随笔分类]个人生活随笔 (14)
- [网站分类]首页候选区 (26)
- [网站分类]SharePoint (15)
- [网站分类]其他技术区 (6)
- [随笔分类]批处理技巧 (6)
- [随笔分类].net 2.0 (3)
- [随笔分类]SharePoint2007(MOSS2007) (0)
- [网站分类].NET新手区 (6)
- [网站分类]ASP.NET (6)
- [网站分类]架构设计 (18)
- [网站分类]程序人生 (2)
- [网站分类]SQL Server (2)
- WCF (3)
- 编程技巧 (2)
- 模式架构 (2)
- 分析设计 (4)
- 生活随笔 (0)
- 软件工程 (1)
- Android实例 (2)
最新评论
-
zilong0536:
楼主您好:
请问发表博文支持图片的功能怎么实现啊,一直没有思路 ...
新浪微博开放平台开发-android客户端(3) -
nicegege:
小弟 学习了
帮助中国移动设计10086的排队小模块 -
zl7824516:
用什么技术没说啊
通告(公告),消息(站内短信),提醒的设计:通告 -
virusswb:
源码下载: SinaWeibo2 源码下载之后,将后缀改为ra ...
新浪微博开放平台开发-android客户端(3) -
Jimmyxu0311:
找不到源码下载
新浪微博开放平台开发-android客户端(3)
在根据总用量计算每种包装规格的购买量和总价 中讲述了一个开发的场景,首先有一个总共的土地拥有量: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
代码如下:
* 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
发表评论
-
NET 应用架构指导 V2 学习笔记(十六) 服务层设计指导
2010-06-04 00:13 527如果你的应用是通 ... -
NET 应用架构指导 V2 学习笔记(十七) 组件设计指导
2010-06-05 00:48 627组件提供了一种将 ... -
NET 应用架构指导 V2 学习笔记(十八) 表现层组件设计指导
2010-06-05 21:09 497本章讲述的是你在设计用户界面组件和表现层逻辑组件的时候应该 ... -
NET 应用架构指导 V2 学习笔记(十九) 表现层组件设计指导
2010-06-06 06:15 5605 决定数据绑定的 ... -
NET 应用架构指导 V2 学习笔记(二十) 业务组件设计指导
2010-06-07 06:58 581前言 业务组件 ... -
微软企业库5.0学习笔记(四十二)异常处理模块
2010-06-14 00:04 808企业库的异常处理 ... -
关于程序员在30岁、35岁之后怎么办的新思考
2010-06-14 10:40 596首先给大家问个好 ... -
NET 应用架构指导 V2 学习笔记(二十四) 跨层关注问题
2010-06-17 20:00 551概况 大部分的 ... -
微软企业库5.0学习笔记(四十三)数据验证模块
2010-06-19 08:07 966概况 任何接受用户或者是其他系统输入的应用,一定要确保 ... -
关于项目进度慢的思考----如何提高整体开发效率
2010-06-21 23:42 774我们都是软件行业 ... -
微软企业库5.0学习笔记(四十四)实战数据验证模块
2010-06-23 19:22 7971 在业务对象上添加验证 添加对程序集【Microso ... -
微软企业库5.0学习笔记(四十五)实战数据验证模块----高级篇
2010-06-24 19:41 9511、添加自定义的提示信息 验证失败的提示信息可以自定义 ... -
面向对象类设计的五大原则(一)单一职责原则Single Responsibility Principle
2010-06-29 15:45 738引言 面向对象类设计,或者说是面向对象设计,有五大原则 ... -
《深入浅出设计模式-中文版》读书笔记 开篇乱弹(一)
2010-07-01 06:42 624oreilly的《Head.First ... -
《深入浅出设计模式-中文版》读书笔记-继承与组合(三)
2010-07-03 16:53 581经过上一次的改造 ... -
《深入浅出设计模式-中文版》读书笔记-观察者模式(四)
2010-07-06 06:34 604今天要接触的是观 ... -
利用attribute实现简单的ORM
2010-07-09 15:27 649我不知道NH的ORM具 ... -
系统内部模块(子系统)之间的耦合以及模块(子系统)划分
2010-07-14 13:02 790题外话 最近已经在努力学习了,学习基本功,学习设计模式 ... -
《深入浅出设计模式-中文版》读书笔记-工厂模式(五)
2010-07-16 12:46 671今天给大家带来的是:工厂模式。 我们在代码中创建一个对 ... -
Head.First.Object-Oriented.Design.and.Analysis《深入浅出面向对象的分析与设计》读书笔记(一)
2010-07-18 21:47 640题外话 又是一本Head.First系列的书,这个系列 ...
相关推荐
SQL语句在审计的简单应用
一款三角网法计算疏浚工程及吹填区吹填工程量计算软件,计算速度很快。
Java Web应用开发 35 课堂案例-调用存储过程统计商品总价.docx 学习资料 复习资料 教学资源
Javascript根据竞拍价格、数量和选择的支付方式计算总价。1. JavaScript的窗体控制 2. DHTML DOM的Form对象 3. DHTML DOM的Input对象 4. DHTML DOM的Select和Option对象 5. DHTML DOM的Textarea对象 6. JavaScript的...
输入苹果的单价及购买的数量,计算总价,分别显示总价的总价整数部分和四舍五入部后的整数部分
用户可以自己规定一个时间作为早期反射声的界限,ease4.0会在standardmapping和render mapping中计算这个反射声。ease4.0中的局部声线跟踪法允许用户在任意一个位置发出一条声线,用来计算局部衰变时间。 ease3.0中...
用组合模式实现当用户在商店购物后,显示其所选商品信息,并计算所选商品总价的功能。 说明:假如王同学到万达生活用品店购物。 5、享元模式 5.1 作业题目 在天猫商城里存在着成千上万的网店,但是天猫所提供的网站...
快速订购与显示购买量与浏览数
使用您喜欢的任何语言,编写一个简单的程序来计算一篮子购物的价格。 项目在列表中一次显示一个,按名称标识 - 例如“Apple”或“Banana”。 多个项目在列表中多次出现,例如 ["Apple", "Apple", "Banana"] 是一个...
报价功能:用户可以根据自己的需求,在系统中选择需要的配件,并生成相应的报价单,系统会根据用户选择的配件自动计算总价格,并提供报价单的下载和打印功能。 配件比较:系统提供了配件参数和价格的比较功能,用户...
jsp的session实现购物车功能,显示商品,购买商品,结算,在MYELIPSE导入即可运行。
世界地图的中英文对照,以及对每个国家按字典序的编号,以字典形式存储,方便由键取值,可用于ECahrts。
看贴功能内浏览了3篇贴子的新用户和仅浏览1篇贴子的新用户进行分析 来自A渠道的新用户进行(有使用看贴/未使用看贴)行为分组比较 渠道对比 是不是某些渠道的量出现问题 用户行为 功能使用及参与度 页面访问...
解决问题 了解产品的付费用户规模 付费用户整体的稳定性 了解付费用户构成 鲸鱼用户、海豚用户、小鱼用户各自数量和比例 注意事项 数据是去重的 ARPU 名词定义 平均每活跃用户收入 统计时间段内,总收入/活跃用户数...
第一行是由空格分隔的两个正整数n和k,分别表示有n种商品和明明购买礼物总重量,第二行为空格分隔的n个正整数,表示现有每种商品的数量,第三行为空格分隔的n个正整数,对应第二行每种商品的总价。 输出说明 一个...
根据售卖咖啡的过程,客户先选择一种口味的咖啡,又选择不同种类的配料,由于配料选择是不定的,符合装饰模式的应用情况。
给定每种图书购买的数量,编程计算应付的总费用。 【输入】 输入一行,包含10个整数(大于等于0,小于等于100),分别表示购买的《计算概论》、《数据结构与算法》、《数字逻辑》、《C++程序设计教程》、《人工智能》...
先根据给出的1000位会员对每种DVD的满意度求出每种DVD 的需求人数,利用第一问虽小购买量与会员 需求量成正比关系的结论,运用计算机模拟的方法,先确定一较小的购买量,再用贪婪算法作为策略进行分 配,计算满意的...
用jsp编写三个页面,一个显示一些历史图书的名称和价格, 一个显示 一些计算机图书的名称和价格 ,另一个 显示为外语书的名称和价格。每本书后面 都有一个链接:购买。单击链接,能够将该本书加到 购物车。每个...
拟开发一个自动取款系统(参考图1-1),它是一个由自动取款机、中央计算机、分行计算机及柜员终端组成的网络系统。ATM和中央计算机由总行投资购买。总行拥有多台ATM,分别设在全市各主要街道上。分行负责提供分行...