数学建模社区-数学中国
标题:
Mathematica 24点程序!
[打印本页]
作者:
cjilyy
时间:
2011-10-14 09:45
标题:
Mathematica 24点程序!
f[x1_, s1_, x2_, s2_, x3_, s3_, x4_] :=
3 W, l( I8 a: }& B8 B9 b5 {9 o* s
Module[{result, i},
0 r3 a. w/ b7 U$ l
result = {HoldForm[(x1~s1~x2)~s2~(x3~s3~x4)],
7 I$ G, \/ p \
HoldForm[x1~s1~(x2~s2~x3~s3~x4)],
: k! q5 Q1 O: L( I# y3 F3 Z
HoldForm[x1~s1~x2~s2~(x3~s3~x4)],
3 a2 n! b7 c9 y: w# |; O
HoldForm[(x1~s1~x2~s2~x3)~s3~x4],
! A$ K4 Y: _% C+ z% V M
HoldForm[x1~s1~(x2~s2~x3)~s3~x4],
* b+ h" l; P4 Y% i* s& j( M( l
HoldForm[(x1~s1~(x2~s2~x3))~s3~x4],
6 w0 S! {; v1 Z; [
HoldForm[x1~s1~(x2~s2~(x3~s3~x4))]};
3 P1 k: I2 f$ S6 f! n I# c
result = Union[result];
) @0 M% J3 w2 j( s) l$ j U" F; X
For[i = 1, i <= Length[result], i++,
+ x N$ c3 \! U' T
If[ReleaseHold[result[[i]]] == 24, Print[result[[i]], "=24"];
& m. K9 |1 F) N; v7 _7 W/ z
(*Print[TreeForm[result[[i]],DirectedEdges->True]]*);];];]
( W7 n6 ?0 H# w' R) X
* _1 x& ^7 C) q+ H( \9 X/ c
fs[x1_, x2_, x3_, x4_] :=
& ?! U7 {4 j1 z1 x4 k: f- o
Module[{result, s, sym, num, i, j}, Off[Divide::infy];
; u% V' L: p7 }% d0 L5 P6 v# z; i5 t
Off[Part::partw];
a) p' o v% i3 B9 e" t
Off[Divide::indet];
Q# S3 h2 U& i& _
s = {Plus, Subtract, Times, Divide};
a: y, i) y& R+ f
sym = Flatten[
7 z) D2 e+ {0 x6 u1 j$ x
Table[{s[[i]], s[[j]], s[[k]]}, {i, 4}, {j, 4}, {k, 4}], 2];
1 v! r9 e. ?1 I3 v. _% D* x
sym = Union[sym];
& C5 P4 D" v4 k; C2 M$ x- O
num = Permutations[{x1, x2, x3, x4}];
s. W$ N0 @- C2 h! \
Do[f[num[[i, 1]], sym[[j, 1]], num[[i, 2]], sym[[j, 2]],
0 g, V$ F$ G# s* \' F
num[[i, 3]], sym[[j, 3]], num[[i, 4]]], {i, Length[num]}, {j,
4 E' [# f1 ]0 Z: y* m' [, }$ c
Length[sym]}]]
! m0 ^" {9 b9 _) Q" E
$ Y" Z( C8 l W7 g: |- n6 P2 `
fs[3, 3, 8, 8]
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5