create table o
(
code char(1) not null
)
insert into o values('+')
insert into o values('-')
insert into o values('*')
insert into o values('/')
gocreate table t
(
id int not null
)
insert into t values(1)
insert into t values(2)
insert into t values(3)
insert into t values(4)
insert into t values(5)
insert into t values(6)
insert into t values(7)
insert into t values(8)
insert into t values(9)
insert into t values(10)
go--第一种sql解法
select distinct * 
from t a, o o1, t b, o o2, t c, o o3, t d 
where 
(case o3.code
when '+' then 
(case o2.code 
when '+' then 
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)+c.id
when '-' then 
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)-c.id
when '*' then 
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)*c.id
when '/' then 
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)/c.id
end)+d.id
when '-' then 
(case o2.code
when '+' then 
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)+c.id
when '-' then 
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)-c.id
when '*' then 
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)*c.id
when '/' then 
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)/c.id
end)-d.id
when '*' then 
(case o2.code
when '+' then 
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)+c.id
when '-' then 
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)-c.id
when '*' then 
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)*c.id
when '/' then 
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)/c.id
end)*d.id
when '/' then 
(case o2.code
when '+' then 
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)+c.id
when '-' then 
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)-c.id
when '*' then 
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)*c.id
when '/' then 
(case o1.code
when '+' then a.id+b.id
when '-' then a.id-b.id
when '*' then a.id*b.id
when '/' then a.id/b.id
end)/c.id
end)/d.id
end)=24

解决方案 »

  1.   

    计算24点,人家有标准答案了:
    http://expert.csdn.net/Expert/topic/2310/2310730.xml?temp=.5600855Create procedure dian24 @a decimal(10,6),
    @b decimal(10,6),
    @c decimal(10,6),
    @d decimal(10,6)
    Asdeclare @t table(id int identity,num decimal(10,2))
    insert @t select @a union all select @b union all select @c union all select @d--生成单项式(A)
    declare @temp1 table(str1 varchar(20),str2 varchar(20),result decimal(10,6))
    --insert @temp1 select rtrim(id),'.'+rtrim(cast(num as int))+'.',num from @t
    insert @temp1 select rtrim(id),rtrim(cast(num as int)),num from @t
    declare @temp2 table(str1 varchar(20),str2 varchar(20),result decimal(10,6))
    declare @temp3 table(str1 varchar(20),str2 varchar(20),result decimal(10,6))
    --生成二项式(A?B))
    insert @temp2 select distinct * from (
    select A.str1+','+B.str1 as str1,A.str2+'+'+B.str2 as str2,A.result+B.result as result from @temp1 A,@temp1 B  where A.str1<>B.str1
    union all
    select A.str1+','+B.str1,A.str2+'-'+B.str2,A.result-B.result from @temp1 A,@temp1 B  where A.str1<>B.str1
    union all
    select A.str1+','+B.str1,B.str2+'-'+A.str2,B.result-A.result from @temp1 A,@temp1 B  where A.str1<>B.str1
    union all
    select A.str1+','+B.str1,A.str2+'*'+B.str2,A.result*B.result from @temp1 A,@temp1 B  where A.str1<>B.str1
    union all
    select A.str1+','+B.str1,A.str2+'/'+B.str2,A.result/B.result from @temp1 A,@temp1 B  where A.str1<>B.str1
    union all
    select A.str1+','+B.str1,B.str2+'/'+A.str2,B.result/A.result from @temp1 A,@temp1 B  where A.str1<>B.str1
    ) M
    --生成三项式(A?B?C)
    insert @temp3 select * from (
    select A.str1+','+B.str1 as str1,A.str2+'+'+B.str2 as str2,A.result+B.result as result from @temp1 A,@temp2 B  where charindex(','+A.str1+',',','+B.str1+',')=0
    union all
    select A.str1+','+B.str1,A.str2+'-'+
         (case when charindex('-',B.str2)>0 or charindex('+',B.str2)>0 then '('+B.str2+')'else B.str2 end),
          A.result-B.result from @temp1 A,@temp2 B  where charindex(','+A.str1+',',','+B.str1+',')=0
    union all
    select A.str1+','+B.str1,B.str2+'-'+A.str2,B.result-A.result from @temp1 A,@temp2 B  where charindex(','+A.str1+',',','+B.str1+',')=0
    union all
    select A.str1+','+B.str1,A.str2+'*'+
           (case when charindex('-',B.str2)>0 or charindex('+',B.str2)>0 then '('+B.str2+')'else B.str2 end),
           A.result*B.result from @temp1 A,@temp2 B  where charindex(','+A.str1+',',','+B.str1+',')=0
    union all
    select A.str1+','+B.str1,A.str2+'/('+B.str2+')',A.result/B.result from @temp1 A,@temp2 B  where charindex(','+A.str1+',',','+B.str1+',')=0 and B.result<>0
    union all
    select A.str1+','+B.str1,
        (case when charindex('-',B.str2)>0 or charindex('+',B.str2)>0 then '('+B.str2+')'else B.str2 end)
         +'/'+A.str2,B.result/A.result from @temp1 A,@temp2 B  where charindex(','+A.str1+',',','+B.str1+',')=0  and A.result<>0
    ) Mselect distinct str2 from (
    --@temp1+@temp3单项式和三项式合并成四项式------
    select A.str1+','+B.str1 as str1,A.str2+'+'+B.str2 as str2,A.result+B.result as result from @temp1 A,@temp3 B  where charindex(','+A.str1+',',','+B.str1+',')=0
    union all
    select A.str1+','+B.str1,A.str2+'-'+
         (case when charindex('-',B.str2)>0 or charindex('+',B.str2)>0 then '('+B.str2+')'else B.str2 end),
          A.result-B.result from @temp1 A,@temp3 B  where charindex(','+A.str1+',',','+B.str1+',')=0
    union all
    select A.str1+','+B.str1,B.str2+'-'+A.str2,B.result-A.result from @temp1 A,@temp3 B  where charindex(','+A.str1+',',','+B.str1+',')=0
    union all
    select A.str1+','+B.str1,A.str2+'*'+
           (case when charindex('-',B.str2)>0 or charindex('+',B.str2)>0 then '('+B.str2+')'else B.str2 end),
           A.result*B.result from @temp1 A,@temp3 B  where charindex(','+A.str1+',',','+B.str1+',')=0
    union all
    select A.str1+','+B.str1,A.str2+'/('+B.str2+')',A.result/B.result from @temp1 A,@temp3 B  where charindex(','+A.str1+',',','+B.str1+',')=0  and B.result<>0
    union all
    select A.str1+','+B.str1,
        (case when charindex('-',B.str2)>0 or charindex('+',B.str2)>0 then '('+B.str2+')'else B.str2 end)
         +'/'+A.str2,B.result/A.result from @temp1 A,@temp3 B  where charindex(','+A.str1+',',','+B.str1+',')=0 and A.result<>0union all
    ------@temp2+@temp2 二项式和二项式合并成四项式------
    select A.str1+','+B.str1 as str1,A.str2+'+'+B.str2 as str2,A.result+B.result as result from @temp2 A,@temp2 B  
    where not exists (select 1 from @temp1 C where charindex(','+str1+',',','+A.str1+',')>0 and charindex(','+str1+',',','+B.str1+',')>0 )union all
    select A.str1+','+B.str1,A.str2+'-'+(case when charindex('-',B.str2)>0 or charindex('+',B.str2)>0 then '('+B.str2+')'else B.str2 end),A.result-B.result 
    from @temp2 A,@temp2 B 
    where not exists (select 1 from @temp1 C where charindex(','+str1+',',','+A.str1+',')>0 and charindex(','+str1+',',','+B.str1+',')>0 )union all
    select A.str1+','+B.str1,B.str2+'-'+
         (case when charindex('-',A.str2)>0 or charindex('+',A.str2)>0 then '('+A.str2+')'else A.str2 end),
    B.result-A.result from @temp2 A,@temp2 B  
    where not exists (select 1 from @temp1 C where charindex(','+str1+',',','+A.str1+',')>0 and charindex(','+str1+',',','+B.str1+',')>0 ) 
    union all
    select A.str1+','+B.str1,
         (case when charindex('-',A.str2)>0 or charindex('+',A.str2)>0 then '('+A.str2+')'else A.str2 end)+'*'+
         (case when charindex('-',B.str2)>0 or charindex('+',B.str2)>0 then '('+B.str2+')'else B.str2 end),
           A.result*B.result from @temp2 A,@temp2 B  
    where not exists (select 1 from @temp1 C where charindex(','+str1+',',','+A.str1+',')>0 and charindex(','+str1+',',','+B.str1+',')>0 ) 
    union all
    select A.str1+','+B.str1,
         (case when charindex('-',A.str2)>0 or charindex('+',A.str2)>0 then '('+A.str2+')'else A.str2 end)
    +'/('+B.str2+')',A.result/B.result from @temp2 A,@temp2 B  
    where not exists (select 1 from @temp1 C where charindex(','+str1+',',','+A.str1+',')>0 and charindex(','+str1+',',','+B.str1+',')>0 )
    and B.result<>0
    union all
    select A.str1+','+B.str1,
        (case when charindex('-',B.str2)>0 or charindex('+',B.str2)>0 then '('+B.str2+')'else B.str2 end)
         +'/('+A.str2+')',B.result/A.result from @temp2 A,@temp2 B  
    where not exists (select 1 from @temp1 C where charindex(','+str1+',',','+A.str1+',')>0 and charindex(','+str1+',',','+B.str1+',')>0 )
    and A.result<>0
     ) M
    where cast(result as decimal(10,4))=24/* Usage:
    exec dian24 3,3,8,8
      

  2.   

    我写过递归的算法,可惜SQL不支持
      

  3.   

    to j9988: 举个例子,漏掉哪些结果了? 另 8/(3-8/3)=?to Jianli2004(健力): 存储过程可以递归!
      

  4.   

    --把 select 子句改为如下有括号的形式
    select distinct '((('+cast(a.id as char(2))+o1.code+cast(b.id as char(2))+')'+o2.code+cast(c.id as char(2))+')'+o3.code+cast(d.id as char(2))+')'--结果
    (((8 *10)/10)*3 )
    (((10-4 )*4 )/1 )
    (((5 +7 )/3 )*6 )
    (((9 *8 )/6 )*2 )
    (((9 *1 )*2 )+6 )
    (((10-7 )*2 )*4 )
    (((8 /7 )*4 )*6 )
    (((7 *5 )-6 )-5 )
    (((5 +10)-3 )*2 )
    (((2 +9 )-8 )*8 )
    (((1 /7 )+4 )*6 )
    (((6 *3 )+6 )/1 )
    (((9 +9 )-3 )+9 )
    (((8 -2 )+2 )*3 )
    (((4 -4 )+4 )*6 )
    (((7 -9 )+8 )*4 )
    (((2 *8 )+7 )+1 )
    (((9 +8 )+9 )-2 )
    (((4 *5 )+6 )-2 )
    (((6 +6 )+2 )+10)
    (((8 +8 )+7 )+1 )
    (((10*1 )+5 )+9 )
    (((2 *10)/3 )*4 )
    (((6 /1 )/2 )*8 )
    (((3 *2 )*2 )*2 )
    (((1 *4 )*2 )*3 )
    (((8 /5 )*8 )*3 )
    (((7 -4 )*10)-6 )
    (((1 +3 )*3 )*2 )
    (((6 -4 )*2 )*6 )
    (((6 *3 )-6 )*2 )
    (((10/4 )*7 )+10)
    (((9 -6 )*9 )-3 )
    (((3 +9 )-6 )*4 ).................
    (((4 +10)/1 )+10)
    (((9 +3 )/3 )*6 )
    (((2 +10)*6 )/3 )
    (((7 +9 )*6 )/4 )
    (((6 *10)*2 )/5 )
    (((7 +1 )*2 )+8 )
    (((8 /4 )*7 )+10)
    (((1 *6 )*5 )-6 )
    (((8 +5 )-7 )*4 )
    (((1 *10)-4 )*4 )
    (((7 +7 )*1 )+10)
    (((5 -3 )+2 )*6 )
    (((7 /1 )+1 )*3 )
    (((4 +9 )-1 )*2 )
    (((5 *3 )+1 )+8 )
    (((5 *2 )+5 )+9 )
    (((4 *7 )+5 )-9 )
    (((6 *2 )/1 )*2 )
    (((10*6 )/2 )-6 )
    (((6 +2 )/2 )*6 )
    (((6 +6 )/4 )*8 )
    (((10/8 )*8 )*3 )
    (((10+2 )*6 )/3 )
    (((10*6 )/3 )+4 )
    (((10/1 )-4 )*4 )
    (((10+9 )*1 )+5 )
    (((4 -3 )*6 )*4 )
    (((4 *10)-6 )-10)
    (((5 +5 )-2 )*3 )
    (((4 +5 )-6 )*8 )
    (((2 /10)+8 )*3 )
    (((8 +9 )-1 )+8 )
    (((8 *4 )-3 )-5 )
    (((6 *5 )+1 )-7 )
    (((6 /2 )+5 )*3 )
    (((4 /7 )+6 )*4 )
    (((7 +8 )+1 )+8 )
    (((4 +9 )+2 )+9 )
    (((7 *3 )+1 )+2 )(所影响的行数为 5137 行)
      

  5.   

    不好意思,我早上没认真看。
    比如:
    你没有3 3 8 8的结果。
    8/(3-8/3)你口算一下是不是24?用计算机int会出错结果不正确。
    主要是正常算24点游戏中是用分数运算。你明白我的意思吗?3           +    3           *    8           /    2
    3           -    3           +    8           *    3
    3           /    3           *    8           *    3
    3           *    3           *    8           /    3
    3           *    3           +    8           +    7
    3           +    3           +    8           +    10你明白我的意思吗?
     8/(3-8/3)=8/(1/3)=24 你明白?
      

  6.   

    还有:你用发括号后,我也说几句。
    4位数四则运算
    括号有多种方式,不是你认为的一种:
    (a?b)?(c?d)
    (a?b?c?)?d
    a?b?(c?d)
    a?b?c?d
    a?(b?c?d)
    ((a?b)?c?)?d
    a?(b?(c?d))
    每种结果都是不一样的。
    还有:不必要的括号要去除。括号只加在必要的地方。
      

  7.   

    --改浮点
    --补漏
    --改用函数后速度变慢了!
    --各位朋友再给瞧瞧
    create table o
    (
    code char(1) not null
    )
    insert into o values('+')
    insert into o values('-')
    insert into o values('*')
    insert into o values('/')
    gocreate table t
    (
    id float not null
    )
    insert into t values(1)
    insert into t values(2)
    insert into t values(3)
    insert into t values(4)
    insert into t values(5)
    insert into t values(6)
    insert into t values(7)
    insert into t values(8)
    insert into t values(9)
    insert into t values(10)
    godrop function f
    go
    create function f(@o char(1), @x float, @y float)
    returns float
    as
    begin
    declare @result float if @o='+'
    set @result=@x+@y
    else
    if @o='-'
    select @result=@x-@y
    else
    if @o='*'
    select @result=@x*@y
    else
    if @o='/'
    begin
    if @y<>0 
    select @result=@x/@y
    else
    select @result=999999999
    end return @result
    end
    go--((a b) c) d
    select a.id a_id, b.id b_id, c.id c_id, d.id d_id, '(('+cast(a.id as char(2))+o1.code+cast(b.id as char(2))+')'+o2.code+cast(c.id as char(2))+')'+o3.code+cast(d.id as char(2)) result
    from t a, o o1, t b, o o2, t c, o o3, t d 
    where abs(dbo.f(o3.code, dbo.f(o2.code, dbo.f(o1.code, a.id, b.id), c.id), d.id)-24)<0.0000000001
    union all
    --a (b (c d))
    select a.id a_id, b.id b_id, c.id c_id, d.id d_id, cast(a.id as char(2))+o1.code+'('+cast(b.id as char(2))+o2.code+'('+cast(c.id as char(2))+o3.code+cast(d.id as char(2))+'))' result
    from t a, o o1, t b, o o2, t c, o o3, t d 
    where abs(dbo.f(o1.code, a.id, dbo.f(o2.code, b.id, dbo.f(o3.code, c.id, d.id)))-24)<0.0000000001
    union all
    --(a (b c)) d
    select a.id a_id, b.id b_id, c.id c_id, d.id d_id, '('+cast(a.id as char(2))+o1.code+'('+cast(b.id as char(2))+o2.code+cast(c.id as char(2))+'))'+o3.code+cast(d.id as char(2)) result
    from t a, o o1, t b, o o2, t c, o o3, t d 
    where abs(dbo.f(o3.code, dbo.f(o1.code, a.id, dbo.f(o2.code, b.id, c.id)), d.id)-24)<0.0000000001
    union all
    --a ((b c) d)
    select a.id a_id, b.id b_id, c.id c_id, d.id d_id, cast(a.id as char(2))+o1.code+'(('+cast(b.id as char(2))+o2.code+cast(c.id as char(2))+')'+o3.code+cast(d.id as char(2))+')' result
    from t a, o o1, t b, o o2, t c, o o3, t d 
    where abs(dbo.f(o1.code, a.id, dbo.f(o3.code, dbo.f(o2.code, b.id, c.id), d.id))-24)<0.0000000001
    union all
    --(a b) (c d)
    select a.id a_id, b.id b_id, c.id c_id, d.id d_id, '('+cast(a.id as char(2))+o1.code+cast(b.id as char(2))+')'+o2.code+'('+cast(c.id as char(2))+o3.code+cast(d.id as char(2))+')' result
    from t a, o o1, t b, o o2, t c, o o3, t d 
    where abs(dbo.f(o2.code, dbo.f(o1.code, a.id, b.id), dbo.f(o3.code, c.id, d.id))-24)<0.0000000001
    order by a_id, b_id, c_id, d_id--结果
    --前四列仅用于排序
    ....................................................8.0 3.0 7.0 7.0 8 *(3 -(7 -7 ))
    8.0 3.0 7.0 7.0 8 *(3 *(7 /7 ))
    8.0 3.0 7.0 7.0 8 *(3 /(7 /7 ))
    8.0 3.0 8.0 1.0 8 /(3 /(8 +1 ))
    8.0 3.0 8.0 3.0 8 /(3 -(8 /3 ))
    8.0 3.0 8.0 5.0 8 +(3 +(8 +5 ))
    8.0 3.0 8.0 7.0 8 *(3 *(8 -7 ))
    8.0 3.0 8.0 7.0 8 *(3 /(8 -7 ))
    8.0 3.0 8.0 8.0 8 *(3 *(8 /8 ))....................................................
      

  8.   

    to Jianli2004(健力) : 此递归非彼递归, 何解?我曾经用 sqlserver 实现了一种手写体数字识别功能(非商业,个人爱好),其中有很多递归阿!
      

  9.   

    to j9988 :
    计算策略有 5 种
    1、((a b) c) d
    2、a (b (c d))
    3、(a (b c)) d
    4、a ((b c) d)
    5、(a b) (c d)你同意吗?
      

  10.   

    --增加函数ff, 增强可读性
    create table o
    (
    code char(1) not null
    )
    insert into o values('+')
    insert into o values('-')
    insert into o values('*')
    insert into o values('/')
    gocreate table t
    (
    id float not null
    )
    insert into t values(1)
    insert into t values(2)
    insert into t values(3)
    insert into t values(4)
    insert into t values(5)
    insert into t values(6)
    insert into t values(7)
    insert into t values(8)
    insert into t values(9)
    insert into t values(10)
    go
    drop function f
    go
    create function f(@o char(1), @x float, @y float)
    returns float
    as
    begin
    declare @result float if @o='+'
    set @result=@x+@y
    else
    if @o='-'
    select @result=@x-@y
    else
    if @o='*'
    select @result=@x*@y
    else
    if @o='/'
    begin
    if @y<>0 
    select @result=@x/@y
    else
    select @result=999999999
    end return @result
    end
    go
    drop function ff
    go
    create function ff(@a float, @o1 char(1), @b float, @o2 char(1), @c float, @o3 char(1), @d float)
    returns varchar(70)
    as
    begin
    declare @result varchar(70)

    set @result='' if abs(dbo.f(@o3, dbo.f(@o2, dbo.f(@o1, @a, @b), @c), @d)-24)<0.0000000001 --((a b) c) d
    set @result=@result+' '+'(('+cast(@a as char(2))+@o1+cast(@b as char(2))+')'+@o2+cast(@c as char(2))+')'+@o3+cast(@d as char(2))
    else
    if abs(dbo.f(@o1, @a, dbo.f(@o2, @b, dbo.f(@o3, @c, @d)))-24)<0.0000000001 --a (b (c d))
    set @result=@result+' '+cast(@a as char(2))+@o1+'('+cast(@b as char(2))+@o2+'('+cast(@c as char(2))+@o3+cast(@d as char(2))+'))'
    else
    if abs(dbo.f(@o3, dbo.f(@o1, @a, dbo.f(@o2, @b, @c)), @d)-24)<0.0000000001 --(a (b c)) d
    set @result=@result+' '+'('+cast(@a as char(2))+@o1+'('+cast(@b as char(2))+@o2+cast(@c as char(2))+'))'+@o3+cast(@d as char(2))
    else
    if abs(dbo.f(@o1, @a, dbo.f(@o3, dbo.f(@o2, @b, @c), @d))-24)<0.0000000001 --a ((b c) d)
    set @result=@result+' '+cast(@a as char(2))+@o1+'(('+cast(@b as char(2))+@o2+cast(@c as char(2))+')'+@o3+cast(@d as char(2))+')'
    else
    if abs(dbo.f(@o2, dbo.f(@o1, @a, @b), dbo.f(@o3, @c, @d))-24)<0.0000000001 --(a b) (c d)
    set @result=@result+' '+'('+cast(@a as char(2))+@o1+cast(@b as char(2))+')'+@o2+'('+cast(@c as char(2))+@o3+cast(@d as char(2))+')' return @result
    end
    go
    select dbo.ff(a.id, o1.code, b.id, o2.code, c.id, o3.code, d.id) result
    from t a, o o1, t b, o o2, t c, o o3, t d
    where dbo.ff(a.id, o1.code, b.id, o2.code, c.id, o3.code, d.id)<>''
    order by a.id, b.id, c.id, d.id--结果................ 8 *(3 /(7 /7 ))
     8 /(3 /(8 +1 ))
     (8 /3 )*(8 +1 )
     8 /(3 -(8 /3 ))
     ((8 +3 )+8 )+5 
     8 *(3 *(8 -7 ))
     8 *(3 /(8 -7 ))................
      

  11.   

    这个不错了,我测试过。
    http://expert.csdn.net/Expert/topic/2310/2310730.xml?temp=.5600855
      

  12.   

    to: jyk1970() 用函数确实是个好方法!!!
     
    我现在没办法测,不知道你是否正确.
    一般来说,你测一下:(5 5 5 1),(3 3 8 8)这两组数有没有答案.不过有个小意见,没用括号要去除,如:
    8.0 3.0 7.0 7.0 8 *(3 *(7 /7 ))--要写成:8*3*(7/7)
      

  13.   

    --函数ff有误,再次更正
    create table o
    (
    code char(1) not null
    )
    insert into o values('+')
    insert into o values('-')
    insert into o values('*')
    insert into o values('/')
    gocreate table t
    (
    id float not null
    )
    insert into t values(1)
    insert into t values(2)
    insert into t values(3)
    insert into t values(4)
    insert into t values(5)
    insert into t values(6)
    insert into t values(7)
    insert into t values(8)
    insert into t values(9)
    insert into t values(10)
    go
    drop function f
    go
    create function f(@o char(1), @x float, @y float)
    returns float
    as
    begin
    declare @result float if @o='+'
    set @result=@x+@y
    else
    if @o='-'
    select @result=@x-@y
    else
    if @o='*'
    select @result=@x*@y
    else
    if @o='/'
    begin
    if @y<>0 
    select @result=@x/@y
    else
    select @result=999999999
    end return @result
    end
    go
    drop function ff
    go
    create function ff(@a float, @o1 char(1), @b float, @o2 char(1), @c float, @o3 char(1), @d float)
    returns varchar(70)
    as
    begin
    declare @result varchar(70)

    set @result='' if abs(dbo.f(@o3, dbo.f(@o2, dbo.f(@o1, @a, @b), @c), @d)-24)<0.0000000001 --((a b) c) d
    set @result=@result+' '+'(('+cast(@a as char(2))+@o1+cast(@b as char(2))+')'+@o2+cast(@c as char(2))+')'+@o3+cast(@d as char(2)) if abs(dbo.f(@o1, @a, dbo.f(@o2, @b, dbo.f(@o3, @c, @d)))-24)<0.0000000001 --a (b (c d))
    set @result=@result+' '+cast(@a as char(2))+@o1+'('+cast(@b as char(2))+@o2+'('+cast(@c as char(2))+@o3+cast(@d as char(2))+'))' if abs(dbo.f(@o3, dbo.f(@o1, @a, dbo.f(@o2, @b, @c)), @d)-24)<0.0000000001 --(a (b c)) d
    set @result=@result+' '+'('+cast(@a as char(2))+@o1+'('+cast(@b as char(2))+@o2+cast(@c as char(2))+'))'+@o3+cast(@d as char(2)) if abs(dbo.f(@o1, @a, dbo.f(@o3, dbo.f(@o2, @b, @c), @d))-24)<0.0000000001 --a ((b c) d)
    set @result=@result+' '+cast(@a as char(2))+@o1+'(('+cast(@b as char(2))+@o2+cast(@c as char(2))+')'+@o3+cast(@d as char(2))+')' if abs(dbo.f(@o2, dbo.f(@o1, @a, @b), dbo.f(@o3, @c, @d))-24)<0.0000000001 --(a b) (c d)
    set @result=@result+' '+'('+cast(@a as char(2))+@o1+cast(@b as char(2))+')'+@o2+'('+cast(@c as char(2))+@o3+cast(@d as char(2))+')' return @result
    end
    go
    select dbo.ff(a.id, o1.code, b.id, o2.code, c.id, o3.code, d.id) result
    from t a, o o1, t b, o o2, t c, o o3, t d
    where dbo.ff(a.id, o1.code, b.id, o2.code, c.id, o3.code, d.id)<>''
    order by a.id, b.id, c.id, d.id
      

  14.   

    结果
    .................................. ((5 *5 )*1 )-1  (5 *(5 *1 ))-1 
     ((5 *5 )+1 )-2  (5 *5 )+(1 -2 )
     (5 *(5 -1 ))+4 
     5 *(5 -(1 /5 )) <---------------------------------
     (5 *(5 +1 ))-6 
     ((5 *5 )-2 )+1 
     (5 *5 )-(2 -1 )
     (5 *5 )-(2 /2 )
     ((5 +5 )+2 )*2  (5 +(5 +2 ))*2 
     ((5 *5 )+2 )-3  (5 *5 )+(2 -3 )
     ((5 +5 )-2 )*3  (5 +(5 -2 ))*3 ................................................. ((8 *3 )/7 )*7  (8 *(3 /7 ))*7  8 *((3 /7 )*7 )
     8 *(3 /(7 /7 )) (8 *3 )/(7 /7 )
     ((8 *3 )*7 )/7  8 *(3 *(7 /7 )) (8 *(3 *7 ))/7  8 *((3 *7 )/7 ) (8 *3 )*(7 /7 )
     (8 /3 )*(8 +1 )
     8 /(3 /(8 +1 ))
     8 /(3 -(8 /3 )) <---------------------------------
     ((8 +3 )+8 )+5  8 +(3 +(8 +5 )) (8 +(3 +8 ))+5  8 +((3 +8 )+5 ) (8 +3 )+(8 +5 )
     8 *(3 *(8 -7 )) (8 *3 )*(8 -7 )
     8 *(3 /(8 -7 )) (8 *3 )/(8 -7 )
     ((8 *3 )/8 )*8  (8 *(3 /8 ))*8  8 *((3 /8 )*8 )
     8 *(3 /(8 /8 )) (8 *3 )/(8 /8 )
     ((8 *3 )*8 )/8  8 *(3 *(8 /8 )) (8 *(3 *8 ))/8  8 *((3 *8 )/8 ) (8 *3 )*(8 /8 )..................................................................................(所影响的行数为 11457 行)
      

  15.   

    而实际上,如果更高要求,3 8 8 8答案只是一个公式:3*8*8/8 ,
    其它所有的都算重复.
    就象两个数:a-b=24一样,如果括号和运算符不吝啬,你可写成:
    a-b
    -(b-a)
    -(-(a-b))
    ........
    可以无穷尽下去.
    而实际上简化了就一个公式:a-b=24,其它都算重复.
    乘除也一样,连乘和连除.或乘除相转化.实际上是同一计算公式.
    继续努力,我等你结果.
      

  16.   

    to  j9988:ok! 容我从长计议!
      

  17.   

    -- 一个漏洞都没有,你看结果
    select dbo.ff(a.id, o1.code, b.id, o2.code, c.id, o3.code, d.id) result
    from t a, o o1, t b, o o2, t c, o o3, t d
    where dbo.ff(a.id, o1.code, b.id, o2.code, c.id, o3.code, d.id)<>''
    and (
    (a.id=3 and b.id=8 and c.id=8 and d.id=8) 
    or (a.id=8 and b.id=3 and c.id=8 and d.id=8)
    or (a.id=8 and b.id=8 and c.id=3 and d.id=8)
    or (a.id=8 and b.id=8 and c.id=8 and d.id=3)
    ) order by a.id, b.id, c.id, d.id--结果
     3 *(8 -(8 -8 )) (3 *8 )-(8 -8 )
     (3 -(8 -8 ))*8 
     ((3 +8 )-8 )*8  (3 +(8 -8 ))*8 
     ((3 *8 )-8 )+8  3 *((8 -8 )+8 )
     ((3 /8 )*8 )*8  (3 /8 )*(8 *8 )
     ((3 *8 )*8 )/8  3 *(8 *(8 /8 )) (3 *(8 *8 ))/8  3 *((8 *8 )/8 ) (3 *8 )*(8 /8 )
     ((3 *8 )/8 )*8  (3 *(8 /8 ))*8  3 *((8 /8 )*8 )
     3 /(8 /(8 *8 )) (3 /(8 /8 ))*8 
     3 *(8 /(8 /8 )) (3 *8 )/(8 /8 )
     3 /((8 /8 )/8 )
     ((3 *8 )+8 )-8  3 *(8 +(8 -8 )) 3 *((8 +8 )-8 ) (3 *8 )+(8 -8 )
     ((3 -8 )+8 )*8 
     8 *(3 -(8 -8 )) (8 *3 )-(8 -8 )
     ((8 +3 )-8 )*8  (8 +(3 -8 ))*8 
     ((8 *3 )-8 )+8  8 *((3 -8 )+8 )
     (8 +(3 *8 ))-8  8 +((3 *8 )-8 )
     ((8 *3 )*8 )/8  8 *(3 *(8 /8 )) (8 *(3 *8 ))/8  8 *((3 *8 )/8 ) (8 *3 )*(8 /8 )
     ((8 *3 )/8 )*8  (8 *(3 /8 ))*8  8 *((3 /8 )*8 )
     8 *(3 /(8 /8 )) (8 *3 )/(8 /8 )
     ((8 *3 )+8 )-8  8 *(3 +(8 -8 )) 8 *((3 +8 )-8 ) (8 *3 )+(8 -8 )
     8 -(8 -(3 *8 )) (8 -(8 -3 ))*8 
     (8 +(8 *3 ))-8  8 +((8 *3 )-8 )
     ((8 /8 )*3 )*8  (8 /8 )*(3 *8 )
     ((8 *8 )*3 )/8  8 *(8 *(3 /8 )) (8 *(8 *3 ))/8  8 *((8 *3 )/8 ) (8 *8 )*(3 /8 )
     8 /(8 /(3 *8 )) (8 /(8 /3 ))*8 
     8 /((8 /3 )/8 )
     8 *(8 +(3 -8 )) 8 *((8 +3 )-8 )
     ((8 -8 )+3 )*8  (8 -8 )+(3 *8 ) <----------------------------------------
     8 *(8 -(8 -3 ))
     8 -(8 -(8 *3 )) (8 -(8 -8 ))*3 
     ((8 +8 )-8 )*3  (8 +(8 -8 ))*3 
     8 *((8 -8 )+3 )
     ((8 /8 )*8 )*3  (8 /8 )*(8 *3 )
     (8 +(8 *8 ))/3 <----------------------------------------
     ((8 *8 )/8 )*3  (8 *(8 /8 ))*3  8 *((8 /8 )*3 )
     8 /(8 /(8 *3 )) (8 /(8 /8 ))*3 
     8 *(8 /(8 /3 )) (8 *8 )/(8 /3 )
     8 /((8 /8 )/3 )
     ((8 -8 )+8 )*3  (8 -8 )+(8 *3 )
     ((8 *8 )+8 )/3 (所影响的行数为 40 行)
      

  18.   

    to j9988 :你应该使用再次更正函数ff