原贴地址:http://topic.csdn.net/u/20100414/13/01a10c02-8230-4f0b-89d1-e0a51a64a97c.html?seed=1728387417
123456789
这九个按顺序排列的数,要求在它们之间插入若干个+,-,*,/ ,
使其结果正好等于如:1*2*3*4+5+6+7*8+9=100
---------------------------------------------------------------------
-- Author : htl258(Tony)
-- Date : 2010-04-19 01:05:31
-- Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 3)
---------------------------------------------------------------------
--游标法
SET NOCOUNT ON
DECLARE @T TABLE(R NVARCHAR(1))
INSERT @T SELECT '+'
UNION ALL SELECT '-'
UNION ALL SELECT '*'
UNION ALL SELECT '/'
DECLARE @S NVARCHAR(50),@I DEC(28,10)
DECLARE @R TABLE(RESULT NVARCHAR(30))
DECLARE C CURSOR FOR
SELECT 'SELECT @I=1.'+A.R+'2.'+B.R+'3.'+C.R+'4.'
+D.R+'5.'+E.R+'6.'+F.R+'7.'+G.R+'8.'+H.R+'9.'
FROM @T A,@T B,@T C,@T D,@T E,@T F,@T G,@T H
OPEN C
FETCH C INTO @S
WHILE @@FETCH_STATUS=0
BEGIN
EXEC SP_EXECUTESQL @S,N'@I DEC(28,10) OUT',@I OUT
IF CEILING(@I)=100 AND FLOOR(@I)=100
INSERT @R SELECT REPLACE(STUFF(@S,1,CHARINDEX('=',@S),''),'.','')
FETCH C INTO @S
END
CLOSE C
DEALLOCATE C
SET NOCOUNT OFF
SELECT * FROM @R
/*
RESULT
------------------------------
1+2+3+4+5+6+7+8*9
1*2*3+4+5+6+7+8*9
1-2*3+4*5+6+7+8*9
1+2*3+4*5-6+7+8*9
1-2*3-4+5*6+7+8*9
1-2*3-4-5+6*7+8*9
1+2+3-4*5+6*7+8*9
1*2*3-4*5+6*7+8*9
1*2*3*4+5+6+7*8+9
1*2*3*4+5+6-7+8*9
1+2-3*4+5*6+7+8*9
1+2-3*4-5+6*7+8*9
1-2+3*4*5+6*7+8-9
1-2+3*4*5-6+7*8-9
1+2*3*4*5/6+7+8*9
(15 行受影响)
*/
--循环法:原著happycell188(喜喜) 改编完善: htl258(Tony)
set nocount on
declare @result nvarchar(50),@temp_int dec(28,10),@str nvarchar(5),
@i1 int,@str1 char(1),
@i2 int,@str2 char(1),
@i3 int,@str3 char(1),
@i4 int,@str4 char(1),
@i5 int,@str5 char(1),
@i6 int,@str6 char(1),
@i7 int,@str7 char(1),
@i8 int,@str8 char(1)
declare @temp_tb table(result nvarchar(30))
--赋值
select @str='+/-*',@i1=1,@i2=1,@i3=1,@i4=1,@i5=1,@i6=1,@i7=1,@i8=1
--函数主体
while(@i1<5)
begin
while(@i2<5)
begin
while(@i3<5)
begin
while(@i4<5)
begin
while(@i5<5)
begin
while(@i6<5)
begin
while(@i7<5)
begin
while(@i8<5)
begin
select
@str1=substring(@str,@i1,1),
@str2=substring(@str,@i2,1),
@str3=substring(@str,@i3,1),
@str4=substring(@str,@i4,1),
@str5=substring(@str,@i5,1),
@str6=substring(@str,@i6,1),
@str7=substring(@str,@i7,1),
@str8=substring(@str,@i8,1)
select @result='select @a=1.'+@str1+'2.'
+@str2+'3.'+@str3+'4.'+@str4+'5.'
+@str5+'6.'+@str6+'7.'+@str7+'8.'+@str8+'9.'
exec sp_executesql @result,
N'@a dec(28,10) output',@temp_int output
if floor(@temp_int)=100 and ceiling(@temp_int)=100
begin
insert into @temp_tb(result)
select right(replace(@result,'.',''),17)
end
select @i8=@i8+1
end
select @i7=@i7+1,@i8=1
end
select @i6=@i6+1,@i7=1,@i8=1
end
select @i5=@i5+1,@i6=1,@i7=1,@i8=1
end
select @i4=@i4+1,@i5=1,@i6=1,@i7=1,@i8=1
end
select @i3=@i3+1,@i4=1,@i5=1,@i6=1,@i7=1,@i8=1
end
select @i2=@i2+1,@i3=1,@i4=1,@i5=1,@i6=1,@i7=1,@i8=1
end
select @i1=@i1+1,@i2=1,@i3=1,@i4=1,@i5=1,@i6=1,@i7=1,@i8=1
end
set nocount off
--获取结果
select result from @temp_tb
/*
result
------------------------------
1+2+3+4+5+6+7+8*9
1+2+3-4*5+6*7+8*9
1+2-3*4+5*6+7+8*9
1+2-3*4-5+6*7+8*9
1+2*3+4*5-6+7+8*9
1+2*3*4*5/6+7+8*9
1-2+3*4*5+6*7+8-9
1-2+3*4*5-6+7*8-9
1-2*3+4*5+6+7+8*9
1-2*3-4+5*6+7+8*9
1-2*3-4-5+6*7+8*9
1*2*3+4+5+6+7+8*9
1*2*3-4*5+6*7+8*9
1*2*3*4+5+6+7*8+9
1*2*3*4+5+6-7+8*9
(15 行受影响)
*/
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/htl258/archive/2010/04/14/5486144.aspx