数学建模社区-数学中国

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

作者: cjilyy    时间: 2011-10-14 09:45
标题: Mathematica 24点程序!
f[x1_, s1_, x2_, s2_, x3_, s3_, x4_] := % K: l( N2 B2 D: Y
Module[{result, i},
' C: k/ A! j8 ^, O. g2 G0 l  result = {HoldForm[(x1~s1~x2)~s2~(x3~s3~x4)],   P: H; M5 q2 {8 l! @6 z
    HoldForm[x1~s1~(x2~s2~x3~s3~x4)],
# ~& {" A7 Q- L3 @0 ]" B' R! x    HoldForm[x1~s1~x2~s2~(x3~s3~x4)], 6 ?" r# ]' y3 T% b8 f. L, \3 D
    HoldForm[(x1~s1~x2~s2~x3)~s3~x4],
: w* ^1 m1 g1 x& f4 @' ]    HoldForm[x1~s1~(x2~s2~x3)~s3~x4],
/ @' d, c' G6 X$ H! i( ~    HoldForm[(x1~s1~(x2~s2~x3))~s3~x4], ( m7 A4 g9 f, ~
    HoldForm[x1~s1~(x2~s2~(x3~s3~x4))]};
7 c! H0 t5 b+ X1 R  result = Union[result];9 L/ U+ H: l- Z8 p: C* G
  For[i = 1, i <= Length[result], i++, / J3 O) y4 J- K# p3 n* |9 f
   If[ReleaseHold[result[[i]]] == 24, Print[result[[i]], "=24"];
3 u9 ], X' k1 y5 u7 e     (*Print[TreeForm[result[[i]],DirectedEdges->True]]*);];];]5 @7 Y# u! n  B/ w+ U
' h+ z: D# l9 Z6 g# \
fs[x1_, x2_, x3_, x4_] := 4 z8 P5 C  b# m# J, c( X
Module[{result, s, sym, num, i, j}, Off[Divide::infy];
. x2 p/ s6 g0 w* s  Off[Part::partw];, N! f8 Y; u2 |' z9 ]
  Off[Divide::indet];0 i$ V0 v4 v0 M2 W7 M
  s = {Plus, Subtract, Times, Divide};
( R+ ?; S# ~+ W  sym = Flatten[6 K4 w+ K" F  ~, N
    Table[{s[[i]], s[[j]], s[[k]]}, {i, 4}, {j, 4}, {k, 4}], 2];0 Y) P6 e; o6 q2 O' b) j
  sym = Union[sym];! ^, y5 X7 c+ X; b
  num = Permutations[{x1, x2, x3, x4}];
6 H' g, f& a4 B% r+ I  Do[f[num[[i, 1]], sym[[j, 1]], num[[i, 2]], sym[[j, 2]],
, E# `# f1 `/ G) c/ {    num[[i, 3]], sym[[j, 3]], num[[i, 4]]], {i, Length[num]}, {j, 8 G0 o8 K( a9 r2 [3 y1 z
    Length[sym]}]]7 v$ w. ?" s9 W  c. G
+ \; B" U% p0 }( g, Q( d* _
fs[3, 3, 8, 8]




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