日期:2014-05-18 浏览次数:20643 次
declare @value int declare @str varchar(100) set @value = 1 while @value<=20 begin ;with cte as ( select @value as MakeValue,a.number as SixNum,b.number as TenNum,c.number as TwentyNum, a.number+b.number+c.number as NumSum,a.number*6+b.number*10+c.number*20 as SumValue from master..spt_values a,master..spt_values b,master..spt_values c where a.[type] = 'p' and b.[type] = 'p' and c.[type] = 'p' and a.number between 0 and 4 and b.number between 0 and 2 and c.number between 0 and 1 and a.number*6+b.number*10+c.number*20 >= @value and a.number*6-@value < 6 and b.number*10-@value < 10 and c.number*20-@value < 20 ) select top 1 @str = ltrim(@value)+'最优箱数排列是:'+'量级6的箱数有'+ltrim(SixNum)+'箱' +',量级为10的箱数有'+ltrim(TenNum)+'箱,量级为20的箱数有'+ltrim(TwentyNum)+'箱' from cte order by SumValue-@value,NumSum print @str set @value = @value + 1 end /******************** 1最优箱数排列是:量级6的箱数有1箱,量级为10的箱数有0箱,量级为20的箱数有0箱 2最优箱数排列是:量级6的箱数有1箱,量级为10的箱数有0箱,量级为20的箱数有0箱 3最优箱数排列是:量级6的箱数有1箱,量级为10的箱数有0箱,量级为20的箱数有0箱 4最优箱数排列是:量级6的箱数有1箱,量级为10的箱数有0箱,量级为20的箱数有0箱 5最优箱数排列是:量级6的箱数有1箱,量级为10的箱数有0箱,量级为20的箱数有0箱 6最优箱数排列是:量级6的箱数有1箱,量级为10的箱数有0箱,量级为20的箱数有0箱 7最优箱数排列是:量级6的箱数有0箱,量级为10的箱数有1箱,量级为20的箱数有0箱 8最优箱数排列是:量级6的箱数有0箱,量级为10的箱数有1箱,量级为20的箱数有0箱 9最优箱数排列是:量级6的箱数有0箱,量级为10的箱数有1箱,量级为20的箱数有0箱 10最优箱数排列是:量级6的箱数有0箱,量级为10的箱数有1箱,量级为20的箱数有0箱 11最优箱数排列是:量级6的箱数有2箱,量级为10的箱数有0箱,量级为20的箱数有0箱 12最优箱数排列是:量级6的箱数有2箱,量级为10的箱数有0箱,量级为20的箱数有0箱 13最优箱数排列是:量级6的箱数有1箱,量级为10的箱数有1箱,量级为20的箱数有0箱 14最优箱数排列是:量级6的箱数有1箱,量级为10的箱数有1箱,量级为20的箱数有0箱 15最优箱数排列是:量级6的箱数有1箱,量级为10的箱数有1箱,量级为20的箱数有0箱 16最优箱数排列是:量级6的箱数有1箱,量级为10的箱数有1箱,量级为20的箱数有0箱 17最优箱数排列是:量级6的箱数有3箱,量级为10的箱数有0箱,量级为20的箱数有0箱 18最优箱数排列是:量级6的箱数有3箱,量级为10的箱数有0箱,量级为20的箱数有0箱 19最优箱数排列是:量级6的箱数有0箱,量级为10的箱数有0箱,量级为20的箱数有1箱 20最优箱数排列是:量级6的箱数有0箱,量级为10的箱数有0箱,量级为20的箱数有1箱
------解决方案--------------------