日期:2014-05-17 浏览次数:20478 次
declare @g int = 1, @m int, @x int, @money int = 100 while (@money > 0) begin set @money = 100 - (@g*5) --剩下的钱 set @m = 1 set @x = 1 --二次循环 while (@m <= @money/3) begin set @x = 100 - @g*5 -@m*3 if @x <> 0 -- 这里不能有0 的情况 print '公鸡:'+convert(varchar(2),@g)+'母鸡:'+convert(varchar(2),@m)+'小鸡:'+convert(varchar(2),@x) set @m = @m +1 end --将母鸡数量重置 set @m = 1 set @money = 100 - (@g*5) set @g = @g+1 end
------解决方案--------------------
应该是百元买百鸡
DECLARE @i INT;
DECLARE @j INT;
DECLARE @k INT;
SET @i = 0;
SET @j = 0;
SET @k = 0;
WHILE @i <= 20
BEGIN
set @j = 0;
WHILE @j <= 33
BEGIN
SET @k =0;
WHILE @k <= 100
BEGIN
IF( @k%3=0 and @i * 5 + @j * 3 + @k/3 = 100 AND @i + @j + @k =100)
begin
PRINT CAST(@i AS VARCHAR) + ' * 5 + ' + Cast(@j AS VARCHAR) + ' * 3 + ' + cast(@k AS VARCHAR) + ' * 1/3'
END
SET @k = @k + 1
END
SET @j = @j + 1
END
SET @i = @i + 1
END
输出:
0 * 5 + 25 * 3 + 75 * 1/3
4 * 5 + 18 * 3 + 78 * 1/3
8 * 5 + 11 * 3 + 81 * 1/3
12 * 5 + 4 * 3 + 84 * 1/3