日期:2014-05-18  浏览次数:20803 次

高分跪求算法
N个浮点数,四舍五入取整后求和与这些数求和后四舍五入取整的最大值

------解决方案--------------------
你这N个浮点数是在一个表里还是一个字符串?
不过也一样了~字符串先换成表,就一样了~
------解决方案--------------------
这样?
declare @f1 float,@f2 float,@f3 float,@f4 float
set @f1 = 2.9224
set @f2 = 4.3208
set @f3 = 8.5848
set @f4 = 7.9598

select
case
when round(@f1,0) + round(@f2,0) +round(@f3,0) +round(@f4,0)> round(@f1+@f2+@f3+@f4,0)
then round(@f1,0) + round(@f2,0) +round(@f3,0) +round(@f4,0)
else round(@f1+@f2+@f3+@f4,0)
end

/*结果
24.0
*/
------解决方案--------------------
create table #aa( a float)
insert into #aa select 100.1
insert into #aa select 20.9
insert into #aa select 20.4
insert into #aa select 20.5
insert into #aa select 20.8
insert into #aa select 20.0

go

select
(case when sum(round(a,0)) <sum(a) then sum(a) else sum(round(a,0)) end ) as suma from #aa

go

drop table #aa
------解决方案--------------------
create table test(col decimal(5,3))
insert test select 2.223
union all select 2.26
union all select 3.4
union all select 6.6
union all select 6.89
union all select 6.93
union all select 6.46

select max(col) from
(
select col=sum(round(col,0)) from test
union all
select round(sum(col),0) from test
)a

drop table test

--
35.000
------解决方案--------------------
declare @f1 varchar(200),@sql varchar(2000)
set @f1= '2.9224,4.3208,8.5848,7.9598 '
set @sql= 'select '
select @sql=@sql+replace(@f1, ', ', ' as a union all select ')
exec( 'select case when sum(round(a,0))> round(sum(a),0) then sum(round(a,0)) else round(sum(a),0) end
from( '+ @sql + ')a ')