最优逼近算法-用SP实现
2007-03-14 19:43
344 查看
name是货名,volume是货物的体积
表T中有N行货物
运输货物的集装箱为68立方米
要求:用存储过程实现的算法来输出一份最大限度利用集装箱空间的货物单
---------------------------------------------------------------------
---------------------------------------------------------------------
create table tt(name varchar(10),volume decimal(9,2))
insert tt select '1','6.24'
union select '3','8.96'
union select '4','8.10'
union select '5','4.37'
union select '6','10.56'
union select '7','50.89'
union select '8','6.24'
union select '9','8.96'
union select '10','8.10'
union select '11','4.37'
union select '12','10.56'
union select '13','50.89'
select * into # from tt where volume<=68
create table #sql(coid int identity(1,1), co varchar(2000))
declare @int decimal(9,2),@max int,@sql varchar(2000),@sqldelete varchar(100)
select @int=max(volume) from #
while(exists(select 1 from #))
begin
select top 1 @sql=name,@sqldelete=name
from # where volume in(select max(volume) from #)
delete # where name=@sqldelete
while(exists(select 1 from # where volume<=68-@int))
begin
set @max=68-@int
select @int=@int+max(volume) from # where volume<=@max
select top 1 @sql=@sql+','+name,@sqldelete=name
from # where volume in(select max(volume) from # where volume<=@max)
delete # where name=@sqldelete
end
insert into #sql values(@sql)
select @int=max(volume) from # where volume<=68
end
select * from #sql
select a.*,b.coid from tt a left join #sql b on CHARINDEX(','+a.name+',',','+b.co+',')>0
order by b.coid
drop table #
drop table #sql
表T中有N行货物
运输货物的集装箱为68立方米
要求:用存储过程实现的算法来输出一份最大限度利用集装箱空间的货物单
---------------------------------------------------------------------
---------------------------------------------------------------------
create table tt(name varchar(10),volume decimal(9,2))
insert tt select '1','6.24'
union select '3','8.96'
union select '4','8.10'
union select '5','4.37'
union select '6','10.56'
union select '7','50.89'
union select '8','6.24'
union select '9','8.96'
union select '10','8.10'
union select '11','4.37'
union select '12','10.56'
union select '13','50.89'
select * into # from tt where volume<=68
create table #sql(coid int identity(1,1), co varchar(2000))
declare @int decimal(9,2),@max int,@sql varchar(2000),@sqldelete varchar(100)
select @int=max(volume) from #
while(exists(select 1 from #))
begin
select top 1 @sql=name,@sqldelete=name
from # where volume in(select max(volume) from #)
delete # where name=@sqldelete
while(exists(select 1 from # where volume<=68-@int))
begin
set @max=68-@int
select @int=@int+max(volume) from # where volume<=@max
select top 1 @sql=@sql+','+name,@sqldelete=name
from # where volume in(select max(volume) from # where volume<=@max)
delete # where name=@sqldelete
end
insert into #sql values(@sql)
select @int=max(volume) from # where volume<=68
end
select * from #sql
select a.*,b.coid from tt a left join #sql b on CHARINDEX(','+a.name+',',','+b.co+',')>0
order by b.coid
drop table #
drop table #sql
相关文章推荐
- 基于CuDNN如何实现自动选取最优卷积算法?
- 从算法入手讲解如何在数据库中实现最优最简
- java实现图的最短路径(SP)的迪杰斯特拉(Dijkstra)算法
- 程序员代码面试指南:IT名企算法与数据结构题目最优解-字符串问题:C/C++语言实现
- 最大N算法--最优资源实现
- String 常用方法最优算法实现总结 (一)
- java实现图的最短路径(SP)的贝尔曼福特(Bellman-Ford)算法
- java实现最大子序列问题——————性能最优的算法
- 使用“初中知识”实现查找重复最优算法 + 最终极限算法
- 商城方式实现最优算法
- Andrew Ng机器学习笔记+Weka相关算法实现(五)SVM最优间隔和核方法
- String 经常用法最优算法实现总结 (一)
- 内存动态分配的首先适应、最优适应、最坏适应算法的实现(java 版)
- 算法java实现--分支限界法--最优装载问题
- bezier逼近的金字塔算法实现
- String 经常用法最优算法实现总结 (二)
- String 常用方法最优算法实现总结 (二)