JavaScript <title>24 Point </title>
<meta name= "Author"
content= "dlpower@163.com">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body>
<br>
Compute 24 Dots (add, subtract, multiply, divide, bracket) JavaScript version v1.0<br><br>
Author: <a
href= ' mailto:dlpower@163.com '>
dlpower@163.com</a> (please indicate the author) <br><br>
2004-04-26 <br><br><br><br>
<form>
Please enter four digits (1-13):<br><br>
<input type= "text" name= "FA" size= "2" >
<input type= "text" name= "FB" size= "2" >
<input type= "text" name= "FC" size= "2" >
<input type= "text" name= "FD" size= "2" >
<br><br><input type= "button" Name= "btn" value= "calculation" >
</form>
<br><br>
<br><br>
In a deck of cards (52), the arbitrary extraction of 4 sheets can have 1820 different combinations, of which 458 are not 24 points.
<br><br>
The mid 1980s, Sun Shijie from Shanghai to the United States to settle. Sun Shijie A few of the neighbors are Americans. Neighbor's <br>
Child to his house, he had no other toys for the children to play, Brainwave, took out a pair of poker to teach them <br>
Play "24 points". Who knows, this play, unexpectedly make American children are fascinated. These kids bring this game home again and <br>
School, it was found that the game was very useful for developing intelligence, which was later promoted throughout the United States.
<script language= "JavaScript" >
function log (str)
{
document.write ("<br>" +str);
}
file:// into the expression of computation
function Genexpress (EXP,A,B,C,D,M1,M2,M3)
{
var exp=exp.replace ("A", a);
Exp=exp.replace ("B", b);
Exp=exp.replace ("C", c);
Exp=exp.replace ("D", D);
Exp=exp.replace ("M1", M1);
Exp=exp.replace ("M2", m2);
Exp=exp.replace ("M3", m3);
return exp;
}
var answer = new Array ()//Expression of correct answer
var counter = number of 0;//answers
file:// The expression of the test is correct
function test (EXPN,A,B,C,D,M1,M2,M3)
{
var exp;
VAR ret;
Exp = genexpress (EXPN,A,B,C,D,M1,M2,M3);//Generate a calculation expression
Eval ("ret =" +exp);
if (Ret-24 < 0.1 && ret-24 >= 0)
{
Exp = Exp.replace (";", "");
Exp = ReplaceAll (exp, "*", "X");
Exp = ReplaceAll (exp, "/", "a");
var have = false;
for (var i=0; i<counter; i++)
{
if (exp = = Answer[i])
{
have = true;
Break
}
}
if (!have)
{
Answer[counter] = exp;
counter++;
Log ("<font color=red><b>" +counter+ ":" +exp+ "</b></font>");
}
}
}
function ReplaceAll (Streng, Soeg, Erstat)
{
var st = Streng;
if (soeg.length = 0)
Return St;
var idx = st.indexof (SOEG);
while (idx >= 0)
{
st = st.substring (0,IDX) + Erstat + st.substr (idx+soeg.length);
IDX = St.indexof (SOEG);
}
Return St;
}
var n = new Array ();//four digits
file:// The number of four input boxes, call the main program
function Funcount ()
{
N[0] = Document.forms[0].fa.value;
N[1] = Document.forms[0].fb.value;
N[2] = Document.forms[0].fc.value;
N[3] = Document.forms[0].fd.value;
if (N[0] > 0 && n[1] > 0 && n[2] > 0 && n[3] > 0 && n[0] < && n [1] < && n[2] < && n[3] < 14)
{
Log ("<font size=5><b>", "+n[0]+", "+n[1]+", "+n[2]+", "+n[3]+" 24-point answer:</b></font><br> ");
Log ("<input type= ' button ' ><br>");
Funmain ();
if (counter = 0)
{
Log ("no answer!") ");
}
}
Else
{
Alert ("Input error!) ");
}
}
file://MasterProgram
function Funmain ()
{
var m = new Array ();
file://FourKinds of operators
M[0] = "+";
M[1] = "-";
M[2] = "*";
M[3] = "/";
FILE://11Type of expression
var exp1 = "A m1 b m2 c M3 D;";
var exp2 = "(a m1 B) m2 C M3 D;";
var exp3 = "(A m1 B m2 c) M3 D;";
var exp4 = "((a M1 B) M2 c) M3 D;";
var exp5 = "(A m1 (b m2 c)) M3 D;";
var exp6 = "A m1 (b m2 c) M3 D;";
var exp7 = "A m1 (b m2 c m3 D);";
var EXP8 = "A m1 ((b m2 c) M3 D);";
var exp9 = "A m1 (b m2 (c m3 D));";
var exp10 = "A m1 b m2 (c m3 D);";
var exp11 = "(a m1 B) m2 (c m3 D);";
var a,b,c,d;//four digits
var m1,m2,m3;//three operators
for (Var i=0;i<4;i++)
{
A = N[i];
for (Var j=0;j<4;j++)
{
if (i = = j) break;//Select a number in the three digits never selected
b = N[j];
for (Var x=0;x<4;x++)
{
if (j = = x) break;//Select a number in the two digits never selected
c = n[x];
for (Var y=0;y<4;y++)
{
if (x = = y) break;//Select a number in a number that has never been selected
d = n[y];
for (Var ta=0;ta<4;ta++)
{
M1 = M[ta];
for (Var tb=0;tb<4;tb++)
{
m2 = M[TB];
for (Var tc=0;tc<4;tc++)
{
M3 = M[TC];
for (Var k=1;k<11;k++)
{
Eval ("Test" (exp "+k+", a,b,c,d,m1,m2,m3); ");
}
}
}
}
}
}
}
}
}
</script>
</body>