数学建模社区-数学中国
标题:
Mathematica 24点程序!
[打印本页]
作者:
cjilyy
时间:
2011-10-14 09:45
标题:
Mathematica 24点程序!
f[x1_, s1_, x2_, s2_, x3_, s3_, x4_] :=
9 U+ J0 E" }0 z
Module[{result, i},
! t; i2 \6 ~) o6 P/ A( N1 [2 s
result = {HoldForm[(x1~s1~x2)~s2~(x3~s3~x4)],
2 @/ }: ~; g8 j* S
HoldForm[x1~s1~(x2~s2~x3~s3~x4)],
# ~( v0 H: o$ f: r
HoldForm[x1~s1~x2~s2~(x3~s3~x4)],
" U3 W) O# U1 J0 O# Q
HoldForm[(x1~s1~x2~s2~x3)~s3~x4],
# t6 _" `: q) }, F2 U& k# a( Y$ a
HoldForm[x1~s1~(x2~s2~x3)~s3~x4],
( r, S. M, p3 H1 A
HoldForm[(x1~s1~(x2~s2~x3))~s3~x4],
/ Y! ?% b0 z3 p$ v- l
HoldForm[x1~s1~(x2~s2~(x3~s3~x4))]};
8 N5 v) Q( Y* m$ I- K
result = Union[result];
) g7 N) \5 N# W6 m B
For[i = 1, i <= Length[result], i++,
0 x" Z, M# u3 c) X, w) j
If[ReleaseHold[result[[i]]] == 24, Print[result[[i]], "=24"];
$ M, Z: N! @( h+ U& O1 t% v
(*Print[TreeForm[result[[i]],DirectedEdges->True]]*);];];]
1 x" V" y% f6 P+ v* |6 {3 Y2 a6 a% ?
u3 h) n6 L2 V" }; |' Y
fs[x1_, x2_, x3_, x4_] :=
; }9 S" c I4 p1 L1 {- H a* |; c
Module[{result, s, sym, num, i, j}, Off[Divide::infy];
, c8 [! E. i; }& h8 k9 [
Off[Part::partw];
3 y" I2 [7 o/ @" k9 ~8 M% x7 y
Off[Divide::indet];
0 z3 _' _& B: @; T% n2 e
s = {Plus, Subtract, Times, Divide};
4 ]( Z0 l9 @6 U( |! z: `* z6 E3 s# ]
sym = Flatten[
/ R8 P3 }3 Y: e4 d7 e8 s% o- p
Table[{s[[i]], s[[j]], s[[k]]}, {i, 4}, {j, 4}, {k, 4}], 2];
9 h, |; `# ~: |
sym = Union[sym];
$ b5 O7 V: v5 |. |* f* G3 {
num = Permutations[{x1, x2, x3, x4}];
# P& I+ P/ {1 p' v* a
Do[f[num[[i, 1]], sym[[j, 1]], num[[i, 2]], sym[[j, 2]],
# l; n9 E1 E& I) E0 A1 F
num[[i, 3]], sym[[j, 3]], num[[i, 4]]], {i, Length[num]}, {j,
f/ I* s+ G2 h* L9 Z
Length[sym]}]]
4 Q$ f! K& v" Y+ ^( J i8 ^
7 p. g! T T2 A, K9 Z: l5 g
fs[3, 3, 8, 8]
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5