讨论 / 可以很快的
sbxiaobai7 2014-01-11 21:27:29
点我顶贴 收藏 删除
3 层搜索,可以很快的。

type

adj = array[1..4] of real;

abc = array[1..4] of boolean;

var

base :adj;

use :abc;

s :string;

ch :char;

i,j :integer;

procedure search(now:adj; use:abc; num:integer);{num 指深度}

var

lin :adj;

i,j,k :integer;

a,b :real;

used :abc;

begin

if num = 4 then exit;

for i := 1 to 4 do lin[i] := now[i];

for i := 1 to 4 do used[i] := use[i];

for i := 1 to 3 do

for j := i+1 to 4 do

for k := 1 to 6 do{有六种情况}

begin

if (used[i]) or (used[j]) then break;{如果某数无效,则退出循环}

a := lin[i];b := lin[j];

case k of

1: begin lin[i] := a*b; used[j] := true; end;

2: begin lin[i] := a-b; used[j] := true; end;

3: begin lin[i] := b-a; used[j] := true; end;

4: begin lin[i] := a+b; used[j] := true; end;

5: if b<>0 then begin lin[i] := a / b; used[j] := true; end;

6: if a<>0 then begin lin[i] := b / a; used[j] := true; end;

end;

if (num = 3)and(abs(lin[i] - 24.0) <= 1e-7) then

begin write('yes'); halt; end

else begin

search(lin, used, num+1);

lin[i] := a; lin[j] := b; used[j] := false;

end;

end;

end;

begin

readln(s);

for i := 1 to length(s) do

begin

ch := s[i];

case ch of

'2'..'9': begin inc(j); base[j]:=ord(ch) - 48; end;

'A': begin inc(j); base[j] := 1; end;

'K': begin inc(j); base[j] := 13; end;

'Q': begin inc(j); base[j] := 12; end;

'J': begin inc(j); base[j] := 11; end;

end;

end;

fillchar(use, sizeof(use), false);

search(base, use, 1);

write('no');

end.

查看更多回复
提交回复