|
|
--不考虑去重, 我的PLSQL方法:
create or replace procedure proc24(p_num1 in pls_integer,
p_num2 in pls_integer,
p_num3 in pls_integer,
p_num4 in pls_integer) is
cursor cur is
with table_operator as
(select decode(substr(op_str, instr(op_str, '/', 1, 1) + 1, 1),
1,
'+',
2,
'-',
3,
'*',
4,
'/') op_1,
decode(substr(op_str, instr(op_str, '/', 1, 2) + 1, 1),
1,
'+',
2,
'-',
3,
'*',
4,
'/') op_2,
decode(substr(op_str, instr(op_str, '/', 1, 3) + 1, 1),
1,
'+',
2,
'-',
3,
'*',
4,
'/') op_3
from (select sys_connect_by_path(rn, '/') op_str
from (select rownum rn from dual connect by level <= 4)
where level = 3
connect by level <= 4)),
table_number as
(select decode(substr(num, instr(num, '/', 1, 1) + 1, 1),
1,
p_num1,
2,
p_num2,
3,
p_num3,
4,
p_num4) num_1,
decode(substr(num, instr(num, '/', 1, 2) + 1, 1),
1,
p_num1,
2,
p_num2,
3,
p_num3,
4,
p_num4) num_2,
decode(substr(num, instr(num, '/', 1, 3) + 1, 1),
1,
p_num1,
2,
p_num2,
3,
p_num3,
4,
p_num4) num_3,
decode(substr(num, instr(num, '/', 1, 4) + 1, 1),
1,
p_num1,
2,
p_num2,
3,
p_num3,
4,
p_num4) num_4
from (select sys_connect_by_path(rn, '/') num
from (select rownum rn from dual connect by level <= 4)
where level = 4
connect by nocycle rn <> prior rn)),
table_parenthesis as
(select 0 locat
from dual
union all
select 12 locat
from dual
union all
select 13 locat
from dual
union all
select 23 locat
from dual
union all
select 24 locat
from dual
union all
select 34 locat
from dual
union all
select 1213 locat
from dual
union all
select 1234 locat
from dual
union all
select 1323 locat
from dual
union all
select 2324 locat
from dual
union all
select 2434 locat from dual)
select num_1, op_1, num_2, op_2, num_3, op_3, num_4, locat
from table_operator, table_number, table_parenthesis;
v_express varchar2(100) := null;
v_result number := 0;
v_num_1 varchar(10) := null;
v_num_2 varchar(10) := null;
v_num_3 varchar(10) := null;
v_num_4 varchar(10) := null;
begin
for rec in cur loop
v_num_1 := null;
v_num_2 := null;
v_num_3 := null;
v_num_4 := null;
v_express := null;
v_result := 0;
if length(rec.locat) = 1 then
v_express := rec.num_1 || rec.op_1 || rec.num_2 || rec.op_2 ||
rec.num_3 || rec.op_3 || rec.num_4;
elsif length(rec.locat) = 2 then
case
when to_number(substr(rec.locat, 1, 1)) = 1 then
v_num_1 := '(' || rec.num_1;
when to_number(substr(rec.locat, 1, 1)) = 2 then
v_num_2 := '(' || rec.num_2;
when to_number(substr(rec.locat, 1, 1)) = 3 then
v_num_3 := '(' || rec.num_3;
end case;
case
when to_number(substr(rec.locat, 2, 1)) = 2 then
v_num_2 := nvl(v_num_2, rec.num_2) || ')';
when to_number(substr(rec.locat, 2, 1)) = 3 then
v_num_3 := nvl(v_num_3, rec.num_3) || ')';
when to_number(substr(rec.locat, 2, 1)) = 4 then
v_num_4 := rec.num_4 || ')';
end case;
v_express := nvl(v_num_1, rec.num_1) || rec.op_1 ||
nvl(v_num_2, rec.num_2) || rec.op_2 ||
nvl(v_num_3, rec.num_3) || rec.op_3 ||
nvl(v_num_4, rec.num_4);
elsif length(rec.locat) = 4 then
case
when to_number(substr(rec.locat, 1, 1)) = 1 then
v_num_1 := '(' || rec.num_1;
when to_number(substr(rec.locat, 1, 1)) = 2 then
v_num_2 := '(' || rec.num_2;
when to_number(substr(rec.locat, 1, 1)) = 3 then
v_num_3 := '(' || rec.num_3;
end case;
case
when to_number(substr(rec.locat, 3, 1)) = 1 then
v_num_1 := '(' || nvl(v_num_1, rec.num_1);
when to_number(substr(rec.locat, 3, 1)) = 2 then
v_num_2 := '(' || nvl(v_num_2, rec.num_2);
when to_number(substr(rec.locat, 3, 1)) = 3 then
v_num_3 := '(' || nvl(v_num_3, rec.num_3);
end case;
case
when to_number(substr(rec.locat, 2, 1)) = 2 then
v_num_2 := nvl(v_num_2, rec.num_2) || ')';
when to_number(substr(rec.locat, 2, 1)) = 3 then
v_num_3 := nvl(v_num_3, rec.num_3) || ')';
when to_number(substr(rec.locat, 2, 1)) = 4 then
v_num_4 := nvl(v_num_4, rec.num_4) || ')';
end case;
case
when to_number(substr(rec.locat, 4, 1)) = 2 then
v_num_2 := nvl(v_num_2, rec.num_2) || ')';
when to_number(substr(rec.locat, 4, 1)) = 3 then
v_num_3 := nvl(v_num_3, rec.num_3) || ')';
when to_number(substr(rec.locat, 4, 1)) = 4 then
v_num_4 := nvl(v_num_4, rec.num_4) || ')';
end case;
v_express := nvl(v_num_1, rec.num_1) || rec.op_1 ||
nvl(v_num_2, rec.num_2) || rec.op_2 ||
nvl(v_num_3, rec.num_3) || rec.op_3 ||
nvl(v_num_4, rec.num_4);
end if;
begin
execute immediate 'select ' || v_express || ' from dual'
into v_result;
exception
when zero_divide then
v_result := 0;
end;
if v_result = 24 then
dbms_output.put_line(v_express || '=24');
end if;
end loop;
end;
SQL> exec proc24(1,2,3,4);
(1+2+3)*4=24
((1+2)+3)*4=24
(1+(2+3))*4=24
(1+3+2)*4=24
((1+3)+2)*4=24
(1+(3+2))*4=24
(2+1+3)*4=24
((2+1)+3)*4=24
(2+(1+3))*4=24
(2+3+1)*4=24
((2+3)+1)*4=24
(2+(3+1))*4=24
(3+1+2)*4=24
((3+1)+2)*4=24
(3+(1+2))*4=24
(3+2+1)*4=24
((3+2)+1)*4=24
(3+(2+1))*4=24
(1+3)*(2+4)=24
(1+3)*(4+2)=24
(2+4)*(1+3)=24
(2+4)*(3+1)=24
(3+1)*(2+4)=24
(3+1)*(4+2)=24
(4+2)*(1+3)=24
(4+2)*(3+1)=24
4*(1+2+3)=24
4*((1+2)+3)=24
4*(1+(2+3))=24
4*(1+3+2)=24
4*((1+3)+2)=24
4*(1+(3+2))=24
4*(2+1+3)=24
4*((2+1)+3)=24
4*(2+(1+3))=24
4*(2+3+1)=24
4*((2+3)+1)=24
4*(2+(3+1))=24
4*(3+1+2)=24
4*((3+1)+2)=24
4*(3+(1+2))=24
4*(3+2+1)=24
4*((3+2)+1)=24
4*(3+(2+1))=24
1*2*3*4=24
(1*2)*3*4=24
(1*2*3)*4=24
1*(2*3)*4=24
1*(2*3*4)=24
1*2*(3*4)=24
((1*2)*3)*4=24
(1*2)*(3*4)=24
(1*(2*3))*4=24
1*((2*3)*4)=24
1*(2*(3*4))=24
1*2*4*3=24
(1*2)*4*3=24
(1*2*4)*3=24
1*(2*4)*3=24
1*(2*4*3)=24
1*2*(4*3)=24
((1*2)*4)*3=24
(1*2)*(4*3)=24
(1*(2*4))*3=24
1*((2*4)*3)=24
1*(2*(4*3))=24
1*3*2*4=24
(1*3)*2*4=24
(1*3*2)*4=24
1*(3*2)*4=24
1*(3*2*4)=24
1*3*(2*4)=24
((1*3)*2)*4=24
(1*3)*(2*4)=24
(1*(3*2))*4=24
1*((3*2)*4)=24
1*(3*(2*4))=24
1*3*4*2=24
(1*3)*4*2=24
(1*3*4)*2=24
1*(3*4)*2=24
1*(3*4*2)=24
1*3*(4*2)=24
((1*3)*4)*2=24
(1*3)*(4*2)=24
(1*(3*4))*2=24
1*((3*4)*2)=24
1*(3*(4*2))=24
1*4*2*3=24
(1*4)*2*3=24
(1*4*2)*3=24
1*(4*2)*3=24
1*(4*2*3)=24
1*4*(2*3)=24
((1*4)*2)*3=24
(1*4)*(2*3)=24
(1*(4*2))*3=24
1*((4*2)*3)=24
1*(4*(2*3))=24
1*4*3*2=24
(1*4)*3*2=24
(1*4*3)*2=24
1*(4*3)*2=24
1*(4*3*2)=24
1*4*(3*2)=24
((1*4)*3)*2=24
(1*4)*(3*2)=24
(1*(4*3))*2=24
1*((4*3)*2)=24
1*(4*(3*2))=24
2*1*3*4=24
(2*1)*3*4=24
(2*1*3)*4=24
2*(1*3)*4=24
2*(1*3*4)=24
2*1*(3*4)=24
((2*1)*3)*4=24
(2*1)*(3*4)=24
(2*(1*3))*4=24
2*((1*3)*4)=24
2*(1*(3*4))=24
2*1*4*3=24
(2*1)*4*3=24
(2*1*4)*3=24
2*(1*4)*3=24
2*(1*4*3)=24
2*1*(4*3)=24
((2*1)*4)*3=24
(2*1)*(4*3)=24
(2*(1*4))*3=24
2*((1*4)*3)=24
2*(1*(4*3))=24
2*3*1*4=24
(2*3)*1*4=24
(2*3*1)*4=24
2*(3*1)*4=24
2*(3*1*4)=24
2*3*(1*4)=24
((2*3)*1)*4=24
(2*3)*(1*4)=24
(2*(3*1))*4=24
2*((3*1)*4)=24
2*(3*(1*4))=24
2*3*4*1=24
(2*3)*4*1=24
(2*3*4)*1=24
2*(3*4)*1=24
2*(3*4*1)=24
2*3*(4*1)=24
((2*3)*4)*1=24
(2*3)*(4*1)=24
(2*(3*4))*1=24
2*((3*4)*1)=24
2*(3*(4*1))=24
2*4*1*3=24
(2*4)*1*3=24
(2*4*1)*3=24
2*(4*1)*3=24
2*(4*1*3)=24
2*4*(1*3)=24
((2*4)*1)*3=24
(2*4)*(1*3)=24
(2*(4*1))*3=24
2*((4*1)*3)=24
2*(4*(1*3))=24
2*4*3*1=24
(2*4)*3*1=24
(2*4*3)*1=24
2*(4*3)*1=24
2*(4*3*1)=24
2*4*(3*1)=24
((2*4)*3)*1=24
(2*4)*(3*1)=24
(2*(4*3))*1=24
2*((4*3)*1)=24
2*(4*(3*1))=24
3*1*2*4=24
(3*1)*2*4=24
(3*1*2)*4=24
3*(1*2)*4=24
3*(1*2*4)=24
3*1*(2*4)=24
((3*1)*2)*4=24
(3*1)*(2*4)=24
(3*(1*2))*4=24
3*((1*2)*4)=24
3*(1*(2*4))=24
3*1*4*2=24
(3*1)*4*2=24
(3*1*4)*2=24
3*(1*4)*2=24
3*(1*4*2)=24
3*1*(4*2)=24
((3*1)*4)*2=24
(3*1)*(4*2)=24
(3*(1*4))*2=24
3*((1*4)*2)=24
3*(1*(4*2))=24
3*2*1*4=24
(3*2)*1*4=24
(3*2*1)*4=24
3*(2*1)*4=24
3*(2*1*4)=24
3*2*(1*4)=24
((3*2)*1)*4=24
(3*2)*(1*4)=24
(3*(2*1))*4=24
3*((2*1)*4)=24
3*(2*(1*4))=24
3*2*4*1=24
(3*2)*4*1=24
(3*2*4)*1=24
3*(2*4)*1=24
3*(2*4*1)=24
3*2*(4*1)=24
((3*2)*4)*1=24
(3*2)*(4*1)=24
(3*(2*4))*1=24
3*((2*4)*1)=24
3*(2*(4*1))=24
3*4*1*2=24
(3*4)*1*2=24
(3*4*1)*2=24
3*(4*1)*2=24
3*(4*1*2)=24
3*4*(1*2)=24
((3*4)*1)*2=24
(3*4)*(1*2)=24
(3*(4*1))*2=24
3*((4*1)*2)=24
3*(4*(1*2))=24
3*4*2*1=24
(3*4)*2*1=24
(3*4*2)*1=24
3*(4*2)*1=24
3*(4*2*1)=24
3*4*(2*1)=24
((3*4)*2)*1=24
(3*4)*(2*1)=24
(3*(4*2))*1=24
3*((4*2)*1)=24
3*(4*(2*1))=24
4*1*2*3=24
(4*1)*2*3=24
(4*1*2)*3=24
4*(1*2)*3=24
4*(1*2*3)=24
4*1*(2*3)=24
((4*1)*2)*3=24
(4*1)*(2*3)=24
(4*(1*2))*3=24
4*((1*2)*3)=24
4*(1*(2*3))=24
4*1*3*2=24
(4*1)*3*2=24
(4*1*3)*2=24
4*(1*3)*2=24
4*(1*3*2)=24
4*1*(3*2)=24
((4*1)*3)*2=24
(4*1)*(3*2)=24
(4*(1*3))*2=24
4*((1*3)*2)=24
4*(1*(3*2))=24
4*2*1*3=24
(4*2)*1*3=24
(4*2*1)*3=24
4*(2*1)*3=24
4*(2*1*3)=24
4*2*(1*3)=24
((4*2)*1)*3=24
(4*2)*(1*3)=24
(4*(2*1))*3=24
4*((2*1)*3)=24
4*(2*(1*3))=24
4*2*3*1=24
(4*2)*3*1=24
(4*2*3)*1=24
4*(2*3)*1=24
4*(2*3*1)=24
4*2*(3*1)=24
((4*2)*3)*1=24
(4*2)*(3*1)=24
(4*(2*3))*1=24
4*((2*3)*1)=24
4*(2*(3*1))=24
4*3*1*2=24
(4*3)*1*2=24
(4*3*1)*2=24
4*(3*1)*2=24
4*(3*1*2)=24
4*3*(1*2)=24
((4*3)*1)*2=24
(4*3)*(1*2)=24
(4*(3*1))*2=24
4*((3*1)*2)=24
4*(3*(1*2))=24
4*3*2*1=24
(4*3)*2*1=24
(4*3*2)*1=24
4*(3*2)*1=24
4*(3*2*1)=24
4*3*(2*1)=24
((4*3)*2)*1=24
(4*3)*(2*1)=24
(4*(3*2))*1=24
4*((3*2)*1)=24
4*(3*(2*1))=24
2*3*4/1=24
(2*3)*4/1=24
(2*3*4)/1=24
2*(3*4)/1=24
2*(3*4/1)=24
2*3*(4/1)=24
((2*3)*4)/1=24
(2*3)*(4/1)=24
(2*(3*4))/1=24
2*((3*4)/1)=24
2*(3*(4/1))=24
2*4*3/1=24
(2*4)*3/1=24
(2*4*3)/1=24
2*(4*3)/1=24
2*(4*3/1)=24
2*4*(3/1)=24
((2*4)*3)/1=24
(2*4)*(3/1)=24
(2*(4*3))/1=24
2*((4*3)/1)=24
2*(4*(3/1))=24
3*2*4/1=24
(3*2)*4/1=24
(3*2*4)/1=24
3*(2*4)/1=24
3*(2*4/1)=24
3*2*(4/1)=24
((3*2)*4)/1=24
(3*2)*(4/1)=24
(3*(2*4))/1=24
3*((2*4)/1)=24
3*(2*(4/1))=24
3*4*2/1=24
(3*4)*2/1=24
(3*4*2)/1=24
3*(4*2)/1=24
3*(4*2/1)=24
3*4*(2/1)=24
((3*4)*2)/1=24
(3*4)*(2/1)=24
(3*(4*2))/1=24
3*((4*2)/1)=24
3*(4*(2/1))=24
4*2*3/1=24
(4*2)*3/1=24
(4*2*3)/1=24
4*(2*3)/1=24
4*(2*3/1)=24
4*2*(3/1)=24
((4*2)*3)/1=24
(4*2)*(3/1)=24
(4*(2*3))/1=24
4*((2*3)/1)=24
4*(2*(3/1))=24
4*3*2/1=24
(4*3)*2/1=24
(4*3*2)/1=24
4*(3*2)/1=24
4*(3*2/1)=24
4*3*(2/1)=24
((4*3)*2)/1=24
(4*3)*(2/1)=24
(4*(3*2))/1=24
4*((3*2)/1)=24
4*(3*(2/1))=24
2*3/1*4=24
(2*3)/1*4=24
(2*3/1)*4=24
2*(3/1)*4=24
2*(3/1*4)=24
((2*3)/1)*4=24
(2*(3/1))*4=24
2*((3/1)*4)=24
2*4/1*3=24
(2*4)/1*3=24
(2*4/1)*3=24
2*(4/1)*3=24
2*(4/1*3)=24
((2*4)/1)*3=24
(2*(4/1))*3=24
2*((4/1)*3)=24
3*2/1*4=24
(3*2)/1*4=24
(3*2/1)*4=24
3*(2/1)*4=24
3*(2/1*4)=24
((3*2)/1)*4=24
(3*(2/1))*4=24
3*((2/1)*4)=24
3*4/1*2=24
(3*4)/1*2=24
(3*4/1)*2=24
3*(4/1)*2=24
3*(4/1*2)=24
((3*4)/1)*2=24
(3*(4/1))*2=24
3*((4/1)*2)=24
4*2/1*3=24
(4*2)/1*3=24
(4*2/1)*3=24
4*(2/1)*3=24
4*(2/1*3)=24
((4*2)/1)*3=24
(4*(2/1))*3=24
4*((2/1)*3)=24
4*3/1*2=24
(4*3)/1*2=24
(4*3/1)*2=24
4*(3/1)*2=24
4*(3/1*2)=24
((4*3)/1)*2=24
(4*(3/1))*2=24
4*((3/1)*2)=24
2*3/(1/4)=24
(2*3)/(1/4)=24
2*(3/(1/4))=24
2*4/(1/3)=24
(2*4)/(1/3)=24
2*(4/(1/3))=24
3*2/(1/4)=24
(3*2)/(1/4)=24
3*(2/(1/4))=24
3*4/(1/2)=24
(3*4)/(1/2)=24
3*(4/(1/2))=24
4*2/(1/3)=24
(4*2)/(1/3)=24
4*(2/(1/3))=24
4*3/(1/2)=24
(4*3)/(1/2)=24
4*(3/(1/2))=24
2/1*3*4=24
(2/1)*3*4=24
(2/1*3)*4=24
2/1*(3*4)=24
((2/1)*3)*4=24
(2/1)*(3*4)=24
2/1*4*3=24
(2/1)*4*3=24
(2/1*4)*3=24
2/1*(4*3)=24
((2/1)*4)*3=24
(2/1)*(4*3)=24
3/1*2*4=24
(3/1)*2*4=24
(3/1*2)*4=24
3/1*(2*4)=24
((3/1)*2)*4=24
(3/1)*(2*4)=24
3/1*4*2=24
(3/1)*4*2=24
(3/1*4)*2=24
3/1*(4*2)=24
((3/1)*4)*2=24
(3/1)*(4*2)=24
4/1*2*3=24
(4/1)*2*3=24
(4/1*2)*3=24
4/1*(2*3)=24
((4/1)*2)*3=24
(4/1)*(2*3)=24
4/1*3*2=24
(4/1)*3*2=24
(4/1*3)*2=24
4/1*(3*2)=24
((4/1)*3)*2=24
(4/1)*(3*2)=24
2/(1/3)*4=24
(2/(1/3))*4=24
2/(1/4)*3=24
(2/(1/4))*3=24
3/(1/2)*4=24
(3/(1/2))*4=24
3/(1/(2*4))=24
3/(1/4)*2=24
(3/(1/4))*2=24
3/(1/(4*2))=24
4/(1/2)*3=24
(4/(1/2))*3=24
4/(1/(2*3))=24
4/(1/3)*2=24
(4/(1/3))*2=24
4/(1/(3*2))=24
3/(1/2/4)=24
3/((1/2)/4)=24
3/(1/4/2)=24
3/((1/4)/2)=24
4/(1/2/3)=24
4/((1/2)/3)=24
4/(1/3/2)=24
4/((1/3)/2)=24
SQL> exec proc24(5,5,5,1);
(5-1/5)*5=24
(5-(1/5))*5=24
(5-1/5)*5=24
(5-(1/5))*5=24
(5-1/5)*5=24
(5-(1/5))*5=24
(5-1/5)*5=24
(5-(1/5))*5=24
(5-1/5)*5=24
(5-(1/5))*5=24
(5-1/5)*5=24
(5-(1/5))*5=24
5*(5-1/5)=24
5*(5-(1/5))=24
5*(5-1/5)=24
5*(5-(1/5))=24
5*(5-1/5)=24
5*(5-(1/5))=24
5*(5-1/5)=24
5*(5-(1/5))=24
5*(5-1/5)=24
5*(5-(1/5))=24
5*(5-1/5)=24
5*(5-(1/5))=24 |
|