Problem with the 24 point algorithm

Source: Internet
Author: User
Tags array expression log version time limit
Algorithm | problem | algorithm HTTP://COMMUNITY.CSDN.NET/EXPERT/TOPIC/3735/3735144.XML?TEMP=8.680362E-02

<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<title>javascript version v1.0</title>
<meta http-equiv= "REFRESH" content=10; Url=*.*>
<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>
<br><br><br><br>
<form>
<div align=center>
Time limit (10 seconds) <br><br>
<input type= "text" name= "fa" value= "<%=l1%>" size= "ten" >
<input type= "text" name= "fb" value= "<%=l2%>" size= "ten" >
<input type= "text" name= "FC" Value= "<%=l3%>" size= "ten" >
<input type= "text" name= "FD" value= "<%=l4%>" size= "ten" >
<br><br><input type= "button" Name= "btn" onclick= "Javascript:funcount ()" value= "View Answer" >

</div></form>


<script language= "JavaScript" >
function log (str)
{
document.write ("<br>" +str);
}

Generate a calculation expression
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

Test expression 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

Receives the number of four input boxes, invokes 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!) ");
}
}

Main program
function Funmain ()
{
var m = new Array ();
Four kinds of operators
M[0] = "+";
M[1] = "-";
M[2] = "*";
M[3] = "/";

11 Kinds of expressions
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); &qu



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.