1樓:機器
24點問題
隨機選四個數(1..10)之間,經過加減乘除(浮點運算)後結果為24.
1..10之間可以隨機重複數字.如可以選4個5.
要求進行括號化簡,並去除重複形式如:3*8+8-8 <==> 3*8-8+8
針對上述條件,可以得到不重複解共1737個。
我這裡有乙個2023年在sqlserver2000下的試驗:
--算符
create table o
(code char(1) not null)go
insert into o values('+')
insert into o values('-')
insert into o values('*')
insert into o values('/')
go--數字1..10
create table t
(id float not null)go
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--優先順序
create table p
(code1 char(1) not null,
code2 char(1) not null,
code3 char(1) not null)go
insert into p
select *, '=' from o o1, o o2 where (o1.code in('+','-') and o2.code in('+','-')) or (o1.
code in('*','/') and o2.code in('*','/'))
union all
select *, '>' from o o1, o o2 where (o1.code in('*','/') and o2.code in('+','-'))
union all
select *, '<' from o o1, o o2 where (o1.code in('+','-') and o2.code in('*','/'))
godrop function 取優先順序
gocreate function 取優先順序(@o1 char(1), @o2 char(1))
returns char(1)
asbegin
declare @r char(1)
select @r=code3 from p where code1=@o1 and code2=@o2
return @r
endgo
--解空間的編號
create table s
(id int not null constraint s_pk primary key clustered)go
--獲取標識求解空間的編號資訊,目的是降低求解空間大小,提高速度
--把排序後完全相同的陣列(4各數組成的),劃分到乙個求解空間
drop function 獲取解空間的編號
gocreate function 獲取解空間的編號(@a int, @b int, @c int, @d int)
returns int
asbegin
declare @r int
set @r=0
select @r=@r*10+id
from
(select @a-1 id
union all
select @b-1
union all
select @c-1
union all
select @d-1
as t
order by id
return @r
endgo
--獲取所有解空間的編號,並存入s表中
insert into s
select distinct dbo.獲取解空間的編號(a.id, b.id, c.id, d.id)
from t a, t b, t c, t d
order by dbo.獲取解空間的編號(a.id, b.id, c.id, d.id)
go--計算模式
create table n
(id int identity(1,1) not null,
model int not null,
o1 char(1) not null,
o2 char(1) not null,
o3 char(1) not null,
v1 char(1) not null,
v2 char(1) not null,
v3 char(1) not null,
v4 char(1) not null,
e char(15) not null, --原始模式
f char(15) not null, --去括號後的模式
g1 int not null,
g2 int not null,
y float not null)go
create index n_idx1 on n(e)
go--計算模式中的變數
create table v
(x char(1) not null)go
insert into v values('a')
insert into v values('b')
insert into v values('c')
insert into v values('d')
go--獲取所有模式,並存入n表中
insert into n
select 1, o1.code, o2.code, o3.
code, v1.x, v2.x, v3.
x, v4.x, '(('+v1.x+o1.
code+v2.x+')'+o2.code+v3.
x+')'+o3.code+v4.x, '', 0, 0, 0
from o o1, o o2, o o3, v v1, v v2, v v3, v v4
where v2.x<>v1.x and (v3.
x<>v1.x and v3.x<>v2.
x) and (v4.x<>v1.x and v4.
x<>v2.x and v4.x<>v3.
x)union all
select 2, o1.code, o2.code, o3.
code, v1.x, v2.x, v3.
x, v4.x, v1.x+o1.
code+'('+v2.x+o2.code+'('+v3.
x+o3.code+v4.x+'))', '', 0, 0, 0
from o o1, o o2, o o3, v v1, v v2, v v3, v v4
where v2.x<>v1.x and (v3.
x<>v1.x and v3.x<>v2.
x) and (v4.x<>v1.x and v4.
x<>v2.x and v4.x<>v3.
x)union all
select 3, o1.code, o2.code, o3.
code, v1.x, v2.x, v3.
x, v4.x, '('+v1.x+o1.
code+'('+v2.x+o2.code+v3.
x+'))'+o3.code+v4.x, '', 0, 0, 0
from o o1, o o2, o o3, v v1, v v2, v v3, v v4
where v2.x<>v1.x and (v3.
x<>v1.x and v3.x<>v2.
x) and (v4.x<>v1.x and v4.
x<>v2.x and v4.x<>v3.
x)union all
select 4, o1.code, o2.code, o3.
code, v1.x, v2.x, v3.
x, v4.x, v1.x+o1.
code+'(('+v2.x+o2.code+v3.
x+')'+o3.code+v4.x+')', '', 0, 0, 0
from o o1, o o2, o o3, v v1, v v2, v v3, v v4
where v2.x<>v1.x and (v3.
x<>v1.x and v3.x<>v2.
x) and (v4.x<>v1.x and v4.
x<>v2.x and v4.x<>v3.
x)union all
select 5, o1.code, o2.code, o3.
code, v1.x, v2.x, v3.
x, v4.x, '('+v1.x+o1.
code+v2.x+')'+o2.code+'('+v3.
x+o3.code+v4.x+')', '', 0, 0, 0
from o o1, o o2, o o3, v v1, v v2, v v3, v v4
where v2.x<>v1.x and (v3.
x<>v1.x and v3.x<>v2.
x) and (v4.x<>v1.x and v4.
x<>v2.x and v4.x<>v3.
x)-- (a o1 b)o2
drop function 去算符左面括號
gocreate function 去算符左面括號(@a varchar(15), @o1 char(1), @b varchar(15), @o2 char(1)) returns varchar(15)
asbegin
return
case
when (@o2='+' or @o2='-') or ((@o2='*' or @o2='/') and (@o1='*' or @o1='/')) then
39; ' + @a + @o1 + @b + ' ' + @o2
else
39;(' + @a + @o1 + @b + ')' + @o2
endend
go-- o1(a o2 b)
drop function 去算符右面括號
gocreate function 去算符右面括號(@o1 char(1), @a varchar(15), @o2 char(1), @b varchar(15)) returns varchar(15)
asbegin
return
case @o1
when '+' then @o1 + ' ' + @a + @o2 +@b + ' '
when '-' then
case
when (@o2='*' or @o2='/') then @o1 + ' ' + @a + @o2 +@b + ' '
when (@o2='+') then @o1 + ' ' + @a + '-' +@b + ' '
when (@o2='-') then @o1 + ' ' + @a + '+' +@b + ' '
endwhen '*' then
case
when (@o2='*' or @o2='/') then @o1 + ' ' + @a + @o2 +@b + ' '
else @o1 + '(' + @a + @o2 +@b + ')'
endwhen '/' then
case
when (@o2='/') then '*' + ' ' + @b + @o2 +@a + ' '
when (@o2='*') then @o1 + ' ' + @b + '/' +@a + ' '
else @o1 + '(' + @a + @o2 +@b + ')'
endend
endgo
drop function 模式的括號化簡
gocreate function 模式的括號化簡(@e varchar(15)) returns varchar(15)
asbegin
declare @r varchar(15)
set @r=replace(@e,' ','')
if substring(@r,1,2)='((' --((a?b)?c)?d
begin
select @r=dbo.去算符左面括號(substring(@r,2,5), substring(@r,7,1), substring(@r,8,1), substring(@r,10,1))+substring(@r,11,1)
select @r=substring(@r,1,1)+dbo.去算符左面括號(substring(@r,3,1), substring(@r,4,1), substring(@r,5,1), substring(@r,7,1)) + substring(@r,8,4)
endelse
if substring(@r,10,2)='))'--a?(b?(c?d))
begin
select @r=substring(@r,1,1)+dbo.去算符右面括號(substring(@r,2,1), substring(@r,4,1), substring(@r,5,1), substring(@r,6,5))
if substring(@r,4,1)<>'('
select @r=substring(@r,1,4)+dbo.去算符右面括號(substring(@r,5,1), substring(@r,7,1), substring(@r,8,1), substring(@r,9,1)) + substring(@r,11,1)
else
select @r=substring(@r,1,3)+dbo.去算符左面括號(substring(@r,5,1), substring(@r,6,1), substring(@r,7,1), substring(@r,9,1)) + substring(@r,10,2)
endelse
if substring(@r,8,2)='))' --(a?(b?c))?d
begin
select @r=dbo.去算符左面括號(substring(@r,2,1), substring(@r,3,1), substring(@r,4,5), substring(@r,10,1))+substring(@r,11,1)
if substring(@r,1,1)='('
select @r=substring(@r,1,2)+dbo.去算符右面括號(substring(@r,3,1), substring(@r,5,1), substring(@r,6,1), substring(@r,7,1)) + substring(@r,9,3
else
begin
if dbo.取優先順序(substring(@r,3,1), substring(@r,10,1))='<'
select @r=substring(@r,1,3)+dbo.去算符左面括號(substring(@r,5,1), substring(@r,6,1), substring(@r,7,1), substring(@r,10,1)) + ' '+ substring(@r,11,1)
else
select @r=substring(@r,1,2)+dbo.去算符右面括號(substring(@r,3,1), substring(@r,5,1), substring(@r,6,1), substring(@r,7,1)) + substring(@r,9,3)
endend
else
if substring(@r,3,2)='((' --a?((b?c)?d)
begin
select @r=substring(@r,1,1)+dbo.去算符右面括號(substring(@r,2,1), substring(@r,4,5), substring(@r,9,1), substring(@r,10,1))
if substring(@r,11,1)=')'
select @r=substring(@r,1,3)+dbo.去算符左面括號(substring(@r,5,1), substring(@r,6,1), substring(@r,7,1), substring(@r,9,1)) + substring(@r,10,2)
else
begin
if substring(@r,4,1)='('
begin
if dbo.取優先順序(substring(@r,2,1), substring(@r,9,1))='<'
select @r=substring(@r,1,3)+dbo.去算符左面括號(substring(@r,5,1), substring(@r,6,1), substring(@r,7,1), substring(@r,9,1)) + substring(@r,10,2)
else
select @r=substring(@r,1,1)+' '+dbo.去算符右面括號(substring(@r,2,1), substring(@r,5,1), substring(@r,6,1), substring(@r,7,1)) + substring(@r,9,3)
endelse
select @r=substring(@r,1,4)+dbo.去算符右面括號(substring(@r,5,1), substring(@r,7,1), substring(@r,8,1), substring(@r,9,1)) + substring(@r,11,1)
endend
elsea?b)?(c?d)
begin
select @r=dbo.去算符左面括號(substring(@r,2,1), substring(@r,3,1), substring(@r,4,1), substring(@r,6,1))+substring(@r,7,5)
select @r=substring(@r,1,5)+dbo.去算符右面括號(substring(@r,6,1), substring(@r,8,1), substring(@r,9,1), substring(@r,10,1))
endreturn @r
endgo
--模式去括號
update n
set f=replace(dbo.模式的括號化簡(e),' ','')
--求值函式
drop function xoy
gocreate function xoy(@o char(1), @x float, @y float)
returns float
asbegin
declare @r float
if abs(@x-10001)<0.0001 or abs(@x-10001)<0.0001
set @r=10001
else
if @o='+'
set @r=@x+@y
else
if @o='-'
set @r=@x-@y
else
if @o='*'
set @r=@x*@y
else
if @o='/'
begin
if @y<>0
set @r=@x/@y
else
set @r=10001
endreturn @r
endgo
--專用的求值函式
drop function get_value
gocreate function get_value(@model int, @v1 char(1), @v2 char(1), @v3 char(1), @v4 char(1), @t1 float, @o1 char(1), @t2 float, @o2 char(1), @t3 float, @o3 char(1), @t4 float)
returns float
asbegin
declare @r float, @a float, @b float, @c float, @d float
set @a=case @v1 when 'a' then @t1 when 'b' then @t2 when 'c' then @t3 else @t4 end
set @b=case @v2 when 'a' then @t1 when 'b' then @t2 when 'c' then @t3 else @t4 end
set @c=case @v3 when 'a' then @t1 when 'b' then @t2 when 'c' then @t3 else @t4 end
set @d=case @v4 when 'a' then @t1 when 'b' then @t2 when 'c' then @t3 else @t4 end
if @model=1 --((a b) c) d
set @r=dbo.xoy(@o3, dbo.xoy(@o2, dbo.xoy(@o1, @a, @b), @c), @d)
else
if @model=2 --a (b (c d))
set @r=dbo.xoy(@o1, @a, dbo.xoy(@o2, @b, dbo.xoy(@o3, @c, @d)))
else
if @model=3 --(a (b c)) d
set @r=dbo.xoy(@o3, dbo.xoy(@o1, @a, dbo.xoy(@o2, @b, @c)), @d)
else
if @model=4 --a ((b c) d)
set @r=dbo.xoy(@o1, @a, dbo.xoy(@o3, dbo.xoy(@o2, @b, @c), @d))
else
if @model=5 --(a b) (c d)
set @r=dbo.xoy(@o2, dbo.xoy(@o1, @a, @b), dbo.xoy(@o3, @c, @d))
return @r
endgo
--根據模式是否等價,進行分組
update n set g1=0
update n set y=dbo.get_value(model, v1, v2, v3, v4, 3.7703346883356315, o1, 5.
8313845011064758, o2, 7.2940727402185634, o3, 8.7109238746920914)
update n set g2=(select top 1 id from n where g1=a.g1 and abs(y-a.y)<0.
00000000001 order by id) from n a
update n set g1=g2
drop function 求解
gocreate function 求解(@a_bh int)
returns @r table(f varchar(15))
asbegin
declare @i int, @row_count int
declare @model int, @a int, @b int, @c int, @d int, @o1 char(1), @o2 char(1), @o3 char(1), @g int, @f varchar(15)
declare @t1 table(sn int, id int)
declare @t2 table(a int, b int, c int, d int)
declare @t3 table(a float, o1 char(1), b float, o2 char(1), c float, o3 char(1), d float)
declare @t4 table(x1 float, x2 float)
declare @t5 table(id int identity(1,1), model int, a float, o1 char(1), b float, o2 char(1), c float, o3 char(1), d float, e varchar(15))
-- 根據求解空間編號計算a,b,c,d
set @a=(@a_bh/1000)
set @b=(@a_bh-@a*1000)/100
set @c=(@a_bh-@a*1000-@b*100)/10
set @d=(@a_bh-@a*1000-@b*100-@c*10)
select @a=@a+1, @b=@b+1, @c=@c+1, @d=@d+1
insert into @t1
select 1, @a
union all
select 2, @b
union all
select 3, @c
union all
select 4, @d
-- 生成可能的排列組合
insert into @t2
select distinct a.id, b.id, c.id, d.id
from @t1 a, @t1 b, @t1 c, @t1 d
where a.sn<>b.sn and (a.
sn<>c.sn and b.sn<>c.
sn) and (a.sn<>d.sn and b.
sn<>d.sn and c.sn<>d.
sn)-- 增加運算符號,生成可能的排列組合
insert into @t3
select distinct x.a, o1.code, x.b, o2.code, x.c, o3.code, x.d
from @t2 x, o o1, o o2, o o3
--((a b) c) d
insert into @t5
select 1, *, '(('+cast(a as varchar(2))+o1+cast(b as varchar(2))+')'+o2+cast(c as varchar(2))+')'+o3+cast(d as varchar(2))
from @t3
where abs(dbo.xoy(o3, dbo.xoy(o2, dbo.xoy(o1, a, b), c), d)-24)<0.00000000001
--a (b (c d))
insert into @t5
select 2, *, cast(a as varchar(2))+o1+'('+cast(b as varchar(2))+o2+'('+cast(c as varchar(2))+o3+cast(d as varchar(2))+'))'
from @t3
where abs(dbo.xoy(o1, a, dbo.xoy(o2, b, dbo.xoy(o3, c, d)))-24)<0.00000000001
--(a (b c)) d
insert into @t5
select 3, *, '('+cast(a as varchar(2))+o1+'('+cast(b as varchar(2))+o2+cast(c as varchar(2))+'))'+o3+cast(d as varchar(2))
from @t3
where abs(dbo.xoy(o3, dbo.xoy(o1, a, dbo.xoy(o2, b, c)), d)-24)<0.00000000001
--a ((b c) d)
insert into @t5
select 4, *, cast(a as varchar(2))+o1+'(('+cast(b as varchar(2))+o2+cast(c as varchar(2))+')'+o3+cast(d as varchar(2))+')'
from @t3
where abs(dbo.xoy(o1, a, dbo.xoy(o3, dbo.xoy(o2, b, c), d))-24)<0.00000000001
--(a b) (c d)
insert into @t5
select 5, *, '('+cast(a as varchar(2))+o1+cast(b as varchar(2))+')'+o2+'('+cast(c as varchar(2))+o3+cast(d as varchar(2))+')'
from @t3
where abs(dbo.xoy(o2, dbo.xoy(o1, a, b), dbo.xoy(o3, c, d))-24)<0.00000000001
select @i=1, @row_count=count(*) from @t5
while @i<=@row_count
begin
select @model=model, @a=a, @b=b, @c=c, @d=d, @o1=o1, @o2=o2, @o3=o3 from @t5 where id=@i
select @g=g1, @f=f from n where model=@model and v1='a' and v2='b' and v3='c' and v4='d' and o1=@o1 and o2=@o2 and o3=@o3
delete from @t5 where e in
select replace(replace(replace(replace(e,'a',@a),'b',@b),'c',@c),'d',@d) from n where g1=@g
and id<>@i
insert into @r select replace(replace(replace(replace(@f,'a',@a),'b',@b),'c',@c),'d',@d)
select @i=min(id) from @t5 where id>@i
endreturn
endgo
drop procedure 求所有解
gocreate procedure 求所有解
asbegin
declare @id int, @id_max int
declare @t table(f varchar(15))
select @id=min(id), @id_max=max(id) from s
while @id<=@id_max
begin
insert into @t
select * from dbo.求解(@id)
select @id=min(id) from s where id>@id
endselect * from @t
endgo--使用方法
select * from dbo.求解(2777) --for 3888
select * from dbo.求解(2277) --for 3388
select * from dbo.求解(0444) --for 1555
求所有解 -- 共 1737 個解
(1+1+1)*8
(1+1)*2*6
(1+1+2)*6
(1+2)*(1+7)
(1*1+2)*8
(1*2+1)*8
(1/1+2)*8
(1+2)*1*8
(1+2)*8/1
(2/1+1)*8
... ...
關於醫學影象增強的演算法?
來了 之前搞眼底影象的血管分割的時候,使用的是VesselNet,github上面有開源的演算法,可以直接呼叫。整體的效果還算可以 BugCreater 當時做這個的目的是提取出來中心線,並且求出中心線的長度。今天搞了一下午,算搞出一點點結果出來。當時上傳的圖找不到了,就貼另外乙個圖了。好了,有一些...
關於演算法的時間複雜度
水dong方塊 演算法時間複雜度分析 選取演算法中一種基本 主要的原操作 取自最深層次迴圈體內的語句 以它重複操作的次數Tn衡量演算法執行時間 n是處理的資料的規模。如果存在兩個常數c1,c2 時則稱Tn和fn 有相同的漸進複雜度 記作 Tn na than 求解演算法的時間複雜度的具體步驟是 找出...
最新的關於目標跟蹤的演算法是哪些?
Tom Hardy 主要是一些特徵提取 濾波類搜尋演算法。其中特徵提取主要有 區域性 全域性特徵 模板 直方圖 binary pattern PCA sparse PCA SR sparse representation discriminative model generative model。對...