数学建模社区-数学中国

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

作者: cjilyy    时间: 2011-10-14 09:45
标题: Mathematica 24点程序!
f[x1_, s1_, x2_, s2_, x3_, s3_, x4_] :=
9 l  F1 n3 t/ B# @9 d Module[{result, i},
' I! r! Y* i6 M! e) H0 \8 _* [  result = {HoldForm[(x1~s1~x2)~s2~(x3~s3~x4)],
9 z& T' _+ G6 y3 R# l$ ^    HoldForm[x1~s1~(x2~s2~x3~s3~x4)], ! [' a7 ~- K- t$ `
    HoldForm[x1~s1~x2~s2~(x3~s3~x4)], " M) F8 z8 F, q; A  D  P/ z* M
    HoldForm[(x1~s1~x2~s2~x3)~s3~x4],
3 J+ O5 \5 h% i9 Q    HoldForm[x1~s1~(x2~s2~x3)~s3~x4],
# H' |2 I2 K2 {1 @* k    HoldForm[(x1~s1~(x2~s2~x3))~s3~x4],
0 C, E4 {- n9 o9 z' g    HoldForm[x1~s1~(x2~s2~(x3~s3~x4))]};; j3 @5 y5 D+ _( g3 D5 d$ y( F% v
  result = Union[result];
% q7 i5 N# O7 Y( w' I: U. K/ W  For[i = 1, i <= Length[result], i++,
* n9 |- Y) l9 v% t% R% W   If[ReleaseHold[result[[i]]] == 24, Print[result[[i]], "=24"];2 X* y4 ?$ t$ C
     (*Print[TreeForm[result[[i]],DirectedEdges->True]]*);];];]
2 ]0 w1 v/ u7 K' D1 L) E  Y) n  p
  c) ?: T5 j. f+ b4 a8 xfs[x1_, x2_, x3_, x4_] := 7 D0 k. M) f- U. C9 ?# |
Module[{result, s, sym, num, i, j}, Off[Divide::infy];# i2 @& v" ^, P: h# S
  Off[Part::partw];
- Q. N% ]7 Q1 W5 \7 L- D: \  Off[Divide::indet];& G' t3 }  L& e, U
  s = {Plus, Subtract, Times, Divide};1 f& E5 C5 S) r; q. B
  sym = Flatten[
: f. u7 B; B: K    Table[{s[[i]], s[[j]], s[[k]]}, {i, 4}, {j, 4}, {k, 4}], 2];
; {- ?9 ^! x0 s: S- m* {  sym = Union[sym];
3 N& J) z9 r3 L& r' D+ g# h! W2 _9 H  num = Permutations[{x1, x2, x3, x4}];
: K' h2 j/ M; E( J; p  Do[f[num[[i, 1]], sym[[j, 1]], num[[i, 2]], sym[[j, 2]],
+ i* m9 Q6 z4 |; j: Y0 X    num[[i, 3]], sym[[j, 3]], num[[i, 4]]], {i, Length[num]}, {j, 0 r0 _1 N+ q( E
    Length[sym]}]]' h/ C; N" l: U3 R  q
) [5 k4 `0 E2 m/ o) Z  D- A; I
fs[3, 3, 8, 8]




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