数学建模社区-数学中国

标题: Mathematica 24点程序! [打印本页]

作者: cjilyy    时间: 2011-10-14 09:45
标题: Mathematica 24点程序!
f[x1_, s1_, x2_, s2_, x3_, s3_, x4_] := 3 U6 ]2 ~- n2 v
Module[{result, i}, . x! p# a' K2 A+ J& H
  result = {HoldForm[(x1~s1~x2)~s2~(x3~s3~x4)],
7 g1 P6 ~7 r7 v, l) ^( F) x  ?    HoldForm[x1~s1~(x2~s2~x3~s3~x4)], 0 D4 f& U; y( K; e
    HoldForm[x1~s1~x2~s2~(x3~s3~x4)],
) l7 h; {) X" w$ k: j    HoldForm[(x1~s1~x2~s2~x3)~s3~x4],
( M/ S$ f1 }& n% |( y! R5 `( J    HoldForm[x1~s1~(x2~s2~x3)~s3~x4],
/ F. i4 z+ s$ w2 B# i; E4 z    HoldForm[(x1~s1~(x2~s2~x3))~s3~x4],
# ~- X$ @! g' ?5 c  ?; `    HoldForm[x1~s1~(x2~s2~(x3~s3~x4))]};
, E9 z1 A/ F+ U# Y+ D3 J  result = Union[result];
2 ?$ b7 |6 N2 d  For[i = 1, i <= Length[result], i++, , L1 c/ V4 Z2 {
   If[ReleaseHold[result[[i]]] == 24, Print[result[[i]], "=24"];7 S" V! o/ G- b, H
     (*Print[TreeForm[result[[i]],DirectedEdges->True]]*);];];]+ p8 J& \& B( x
9 r6 x' u& t* }1 z, `
fs[x1_, x2_, x3_, x4_] :=
+ p+ p; B3 E* B4 Z, z, R Module[{result, s, sym, num, i, j}, Off[Divide::infy];
3 R: i, v9 C1 D  Off[Part::partw];' x! f8 |, n; Q5 f) p- g) A
  Off[Divide::indet];
/ j+ q) x4 Y9 {; e! y# j  s = {Plus, Subtract, Times, Divide};
1 m! Z7 }/ ^6 `! i/ Y  sym = Flatten[; W- ~' e- _# J7 b, g
    Table[{s[[i]], s[[j]], s[[k]]}, {i, 4}, {j, 4}, {k, 4}], 2];
' l0 x6 b1 P: b3 @  sym = Union[sym];* L, z* O7 p1 u* D, D) ~, b: w
  num = Permutations[{x1, x2, x3, x4}];
$ ~4 n! a/ p3 ]: N9 ~  Do[f[num[[i, 1]], sym[[j, 1]], num[[i, 2]], sym[[j, 2]],
/ M$ f% |3 f3 }( }5 Q( L% W    num[[i, 3]], sym[[j, 3]], num[[i, 4]]], {i, Length[num]}, {j,
4 b2 ^& `# V, q' h( U7 h    Length[sym]}]]1 ?7 y/ o+ s1 y7 r5 E

9 U/ q6 Q2 A6 d( M7 C" a" |fs[3, 3, 8, 8]




欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5