select number into #a from master..spt_values where xtype='p' and number between 1 and 100select a.x,b.x y,c.x z from #a a,#a b,#a c where 5*a.x+3*b.x+c.x=100
select number as x into #a from master..spt_values where xtype='p' and number between 1 and 100select a.x,b.x y,c.x z from #a a,#a b,#a c where 5*a.x+3*b.x+c.x=100
还是有点问题,大家不要被我误导了,应该是一个3重循环在C中可以这样实现,for (int i = 0; i < 100; i++) for (int j = 0; j < 100; j++) for (int k = 0; k < 100; k++) { if (i * 5 + j * 3 + k*1.0/3 = 100) { if (i + j + k = 100) { printf("%d %d %d",i,j,k); } } }
#2测试如下:注意修改了红色字体处 select number as x into #a from master..spt_values where type='p' and number between 1 and 100select a.x,b.x y,c.x z from #a a,#a b,#a c where 5*a.x+3*b.x+c.x/3=100
declare @gongji int=0,@muji int=0,@xiaoji int=0 while @gongji<100 begin while @muji<100 begin while @xiaoji<100 begin if(5*@gongji+3*@muji+1*@xiaoji/3=100) and @gongji+@muji+@xiaoji=100 print N'公鸡:'+rtrim(@gongji)+N' 母鸡:'+rtrim(@muji)+N' 小鸡:'+rtrim(@xiaoji) set @xiaoji=@xiaoji+1 end select @xiaoji=0,@muji=@muji+1 end select @gongji=@gongji+1,@muji=0,@xiaoji=0 end
不好意思 ,继续修改后如下:注意修改了红色字体处 select number as x into #a from master..spt_values where type='p' and number between 1 and 100select a.x,b.x y,c.x z from #a a,#a b,#a c where 5*a.x+3*b.x+c.x/3.0=100
TKS,题目看错了。select number as x into #a from master..spt_values where type='p' and number between 1 and 100select a.x,b.x y,c.x z from #a a,#a b,#a c where 5*a.x*3+3*b.x*3+c.x=300改成乘法。用除法是有差异的。
请各位大侠帮忙看看,为啥这种方法行不通呢?DECLARE @I INT,@J INT,@K INT create table #TB(I int,J int,K int) SELECT @I=0,@J=0,@K=0 WHILE @I<100 BEGIN WHILE @J<100 BEGIN WHILE @K<100 BEGIN IF(@I*5+@J*3+@K*1.0/3=100) BEGIN --PRINT(@I+' '+@J+' '+@K);
insert into #TB(I,J,K) values(@I,@J,@K) END SET @K=@K+1 END SET @J=@J+1 END SET @I=@I+1 END select * from #TB
为方便各位看,重新发一遍了 DECLARE @I INT,@J INT,@K INT create table #TB(I int,J int,K int) SELECT @I=0,@J=0,@K=0 WHILE @I<100 BEGIN WHILE @J<100 BEGIN WHILE @K<100 BEGIN IF(@I*5+@J*3+@K*1.0/3=100) BEGIN --PRINT(@I+' '+@J+' '+@K);
insert into #TB(I,J,K) values(@I,@J,@K) END SET @K=@K+1 END SET @J=@J+1 END SET @I=@I+1 END select * from #TB
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
从题目上看:并没有说 1) 公鸡、母鸡和小鸡总数为100, 也没有说 2) 公鸡、母鸡和小鸡数不能为0 在此,我总结了下大家的回答。楼主是否满意呢?---方法一:SQL77的方法 --drop table #a select number as x into #a from master..spt_values where type='p' and number between 0 and 100--把0到100的数据放入表#a select a.x,b.x y,c.x z from #a a,#a b,#a c where 5*a.x+3*b.x+1.0*c.x/3=100 --筛选出符合要求的组合数--方法二:ssp2009的方法 --DROP TABLE #TB DECLARE @I INT,@J INT,@K INT CREATE TABLE #TB(I INT,J INT,K INT) SELECT @I=0,@J=0,@K=0 WHILE @I<100 BEGIN WHILE @J<100 BEGIN WHILE @K<100 BEGIN IF(@I*5+@J*3+@K*1.0/3=100) BEGIN INSERT INTO #TB(I,J,K) VALUES(@I,@J,@K) END SET @K=@K+1 END SELECT @J=@J+1,@K=0 END SELECT @I=@I+1,@J=0,@K=0 END --SELECT * FROM #TB WHERE I=0 or J=0 or K=0
declare @gongji int declare @muji int declare @xiaoji int declare @t table(gongji int,muji int,xiaoji int) set @gongji=0 set @muji=0 set @xiaoji=0 while @gongji<21 begin while @muji<34 begin if(100-@gongji*5-@muji*3>=0) begin insert into @t values(@gongji,@muji,(100-@gongji*5-@muji*3)*3) end set @muji=@muji+1 end set @gongji=@gongji+1 set @muji=0 end select * from @t
/*声明能买的范围表*/ create table #TableNum (x int ) declare @Num1 int declare @Num2 int select @Num1=0 --至少要买 select @Num2=300--最多能买 while @Num1<=@Num2 begin insert into #TableNum(x) values(@Num1) select @Num1=@Num1+1 end select a.x 公鸡,b.x 母鸡,c.x 小鸡 from #TableNum a,#TableNum b,#TableNum c where a.x<=20 and b.x<=33 and c.x<=300 and 15*a.x+9*b.x+c.x=300 order by a.x,b.x drop table #TableNum
/*声明能买的范围表*/ create table #TableNum (x int ) declare @Num1 int declare @Num2 int select @Num1=0 --至少要买 select @Num2=300--最多能买 while @Num1<=@Num2 begin insert into #TableNum(x) values(@Num1) select @Num1=@Num1+1 end select a.x 公鸡,b.x 母鸡,c.x 小鸡 from #TableNum a,#TableNum b,#TableNum c where a.x<=20 and b.x<=33 and c.x<=300 and 15*a.x+9*b.x+c.x=300 and a.x+b.x+c.x=100--和起来一共100只鸡 order by a.x,b.x drop table #TableNum(4 行受影响)
from master..spt_values where xtype='p' and number between 1 and 100select a.x,b.x y,c.x z from #a a,#a b,#a c where 5*a.x+3*b.x+c.x=100
from master..spt_values where xtype='p' and number between 1 and 100select a.x,b.x y,c.x z from #a a,#a b,#a c where 5*a.x+3*b.x+c.x=100
for (int j = 0; j < 100; j++)
for (int k = 0; k < 100; k++)
{
if (i * 5 + j * 3 + k*1.0/3 = 100)
{
if (i + j + k = 100)
{
printf("%d %d %d",i,j,k);
}
}
}
select number as x into #a
from master..spt_values where type='p' and number between 1 and 100select a.x,b.x y,c.x z from #a a,#a b,#a c where 5*a.x+3*b.x+c.x=100(100 行受影响)
x y z
----------- ----------- -----------
17 2 9
15 2 19
13 2 29
11 2 39
9 2 49
7 2 59
5 2 69
3 2 79
1 2 89
17 4 3
15 4 13
13 4 23
11 4 33
9 4 43
7 4 53
5 4 63
3 4 73
1 4 83
15 6 7
13 6 17
11 6 27
9 6 37
7 6 47
5 6 57
3 6 67
1 6 77
15 8 1
13 8 11
11 8 21
9 8 31
7 8 41
5 8 51
3 8 61
1 8 71
13 10 5
11 10 15
9 10 25
7 10 35
5 10 45
3 10 55
1 10 65
11 12 9
9 12 19
7 12 29
5 12 39
3 12 49
1 12 59
11 14 3
9 14 13
7 14 23
5 14 33
3 14 43
1 14 53
9 16 7
7 16 17
5 16 27
3 16 37
1 16 47
9 18 1
7 18 11
5 18 21
3 18 31
1 18 41
7 20 5
5 20 15
3 20 25
1 20 35
5 22 9
3 22 19
1 22 29
5 24 3
3 24 13
1 24 23
3 26 7
1 26 17
3 28 1
1 28 11
1 30 5
19 1 2
17 1 12
15 1 22
13 1 32
11 1 42
9 1 52
7 1 62
5 1 72
3 1 82
1 1 92
17 3 6
15 3 16
13 3 26
11 3 36
9 3 46
7 3 56
5 3 66
3 3 76
1 3 86
15 5 10
13 5 20
11 5 30
9 5 40
7 5 50
5 5 60
3 5 70
1 5 80
15 7 4
13 7 14
11 7 24
9 7 34
7 7 44
5 7 54
3 7 64
1 7 74
13 9 8
11 9 18
9 9 28
7 9 38
5 9 48
3 9 58
1 9 68
13 11 2
11 11 12
9 11 22
7 11 32
5 11 42
3 11 52
1 11 62
11 13 6
9 13 16
7 13 26
5 13 36
3 13 46
1 13 56
9 15 10
7 15 20
5 15 30
3 15 40
1 15 50
9 17 4
7 17 14
5 17 24
3 17 34
1 17 44
7 19 8
5 19 18
3 19 28
1 19 38
7 21 2
5 21 12
3 21 22
1 21 32
5 23 6
3 23 16
1 23 26
3 25 10
1 25 20
3 27 4
1 27 14
1 29 8
1 31 2
18 2 4
16 2 14
14 2 24
12 2 34
10 2 44
8 2 54
6 2 64
4 2 74
2 2 84
16 4 8
14 4 18
12 4 28
10 4 38
8 4 48
6 4 58
4 4 68
2 4 78
16 6 2
14 6 12
12 6 22
10 6 32
8 6 42
6 6 52
4 6 62
2 6 72
14 8 6
12 8 16
10 8 26
8 8 36
6 8 46
4 8 56
2 8 66
12 10 10
10 10 20
8 10 30
6 10 40
4 10 50
2 10 60
12 12 4
10 12 14
8 12 24
6 12 34
4 12 44
2 12 54
10 14 8
8 14 18
6 14 28
4 14 38
2 14 48
10 16 2
8 16 12
6 16 22
4 16 32
2 16 42
8 18 6
6 18 16
4 18 26
2 18 36
6 20 10
4 20 20
2 20 30
6 22 4
4 22 14
2 22 24
4 24 8
2 24 18
4 26 2
2 26 12
2 28 6
18 1 7
16 1 17
14 1 27
12 1 37
10 1 47
8 1 57
6 1 67
4 1 77
2 1 87
18 3 1
16 3 11
14 3 21
12 3 31
10 3 41
8 3 51
6 3 61
4 3 71
2 3 81
16 5 5
14 5 15
12 5 25
10 5 35
8 5 45
6 5 55
4 5 65
2 5 75
14 7 9
12 7 19
10 7 29
8 7 39
6 7 49
4 7 59
2 7 69
14 9 3
12 9 13
10 9 23
8 9 33
6 9 43
4 9 53
2 9 63
12 11 7
10 11 17
8 11 27
6 11 37
4 11 47
2 11 57
12 13 1
10 13 11
8 13 21
6 13 31
4 13 41
2 13 51
10 15 5
8 15 15
6 15 25
4 15 35
2 15 45
8 17 9
6 17 19
4 17 29
2 17 39
8 19 3
6 19 13
4 19 23
2 19 33
6 21 7
4 21 17
2 21 27
6 23 1
4 23 11
2 23 21
4 25 5
2 25 15
2 27 9
2 29 3(304 行受影响)
select number as x into #a
from master..spt_values where type='p' and number between 1 and 100select a.x,b.x y,c.x z from #a a,#a b,#a c where 5*a.x+3*b.x+c.x/3=100
while @gongji<100
begin
while @muji<100
begin
while @xiaoji<100
begin
if(5*@gongji+3*@muji+1*@xiaoji/3=100) and @gongji+@muji+@xiaoji=100
print N'公鸡:'+rtrim(@gongji)+N' 母鸡:'+rtrim(@muji)+N' 小鸡:'+rtrim(@xiaoji)
set @xiaoji=@xiaoji+1
end
select @xiaoji=0,@muji=@muji+1
end
select @gongji=@gongji+1,@muji=0,@xiaoji=0
end
select number as x into #a
from master..spt_values where type='p' and number between 1 and 100select a.x,b.x y,c.x z from #a a,#a b,#a c where 5*a.x+3*b.x+c.x/3.0=100
from master..spt_values where type='p' and number between 1 and 100select a.x,b.x y,c.x z from #a a,#a b,#a c where 5*a.x*3+3*b.x*3+c.x=300改成乘法。用除法是有差异的。
create table #TB(I int,J int,K int)
SELECT @I=0,@J=0,@K=0
WHILE @I<100
BEGIN
WHILE @J<100
BEGIN
WHILE @K<100
BEGIN
IF(@I*5+@J*3+@K*1.0/3=100)
BEGIN
--PRINT(@I+' '+@J+' '+@K);
insert into #TB(I,J,K) values(@I,@J,@K)
END
SET @K=@K+1
END
SET @J=@J+1
END
SET @I=@I+1
END
select * from #TB
DECLARE @I INT,@J INT,@K INT
create table #TB(I int,J int,K int)
SELECT @I=0,@J=0,@K=0
WHILE @I<100
BEGIN
WHILE @J<100
BEGIN
WHILE @K<100
BEGIN
IF(@I*5+@J*3+@K*1.0/3=100)
BEGIN
--PRINT(@I+' '+@J+' '+@K);
insert into #TB(I,J,K) values(@I,@J,@K)
END
SET @K=@K+1
END
SET @J=@J+1
END
SET @I=@I+1
END
select * from #TB
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
也没有说 2) 公鸡、母鸡和小鸡数不能为0
在此,我总结了下大家的回答。楼主是否满意呢?---方法一:SQL77的方法
--drop table #a
select number as x into #a
from master..spt_values where type='p' and number between 0 and 100--把0到100的数据放入表#a
select a.x,b.x y,c.x z from #a a,#a b,#a c where 5*a.x+3*b.x+1.0*c.x/3=100 --筛选出符合要求的组合数--方法二:ssp2009的方法
--DROP TABLE #TB
DECLARE @I INT,@J INT,@K INT
CREATE TABLE #TB(I INT,J INT,K INT)
SELECT @I=0,@J=0,@K=0
WHILE @I<100
BEGIN
WHILE @J<100
BEGIN
WHILE @K<100
BEGIN
IF(@I*5+@J*3+@K*1.0/3=100)
BEGIN
INSERT INTO #TB(I,J,K) VALUES(@I,@J,@K)
END
SET @K=@K+1
END
SELECT @J=@J+1,@K=0
END
SELECT @I=@I+1,@J=0,@K=0
END
--SELECT * FROM #TB WHERE I=0 or J=0 or K=0
declare @gongji int
declare @muji int
declare @xiaoji int
declare @t table(gongji int,muji int,xiaoji int)
set @gongji=0
set @muji=0
set @xiaoji=0
while @gongji<21
begin
while @muji<34
begin
if(100-@gongji*5-@muji*3>=0)
begin
insert into @t values(@gongji,@muji,(100-@gongji*5-@muji*3)*3)
end
set @muji=@muji+1
end
set @gongji=@gongji+1
set @muji=0
end
select * from @t
/*声明能买的范围表*/
create table #TableNum
(x int )
declare @Num1 int
declare @Num2 int
select @Num1=0 --至少要买
select @Num2=300--最多能买
while @Num1<=@Num2
begin
insert into #TableNum(x) values(@Num1)
select @Num1=@Num1+1
end
select a.x 公鸡,b.x 母鸡,c.x 小鸡 from #TableNum a,#TableNum b,#TableNum c
where
a.x<=20
and b.x<=33
and c.x<=300
and 15*a.x+9*b.x+c.x=300
order by a.x,b.x
drop table #TableNum
/*声明能买的范围表*/
create table #TableNum
(x int )
declare @Num1 int
declare @Num2 int
select @Num1=0 --至少要买
select @Num2=300--最多能买
while @Num1<=@Num2
begin
insert into #TableNum(x) values(@Num1)
select @Num1=@Num1+1
end
select a.x 公鸡,b.x 母鸡,c.x 小鸡 from #TableNum a,#TableNum b,#TableNum c
where
a.x<=20
and b.x<=33
and c.x<=300
and 15*a.x+9*b.x+c.x=300
and a.x+b.x+c.x=100--和起来一共100只鸡
order by a.x,b.x
drop table #TableNum(4 行受影响)