24点算法有问题
网络整理 - 08-16
http://community.csdn.net/Expert/topic/3735/3735144.xml?temp=8.680362E-02<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Javascript版 v1.0</title>
<META HTTP-EQUIV="REFRESH" CONTENT=10; URL=*.*>
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
</head>
<body>
<br>
计算24点(加、减、乘、除、括号) Javascript版 v1.0<br><br>
<br><br><br><br>
<form>
<div align=center>
时间限制(10秒)<br><br>
<input type="text" name="fa" value="<%=l1%>" size="10">
<input type="text" name="fb" value="<%=l2%>" size="10">
<input type="text" name="fc" value="<%=l3%>" size="10">
<input type="text" name="fd" value="<%=l4%>" size="10">
<br><br><input type="button" name="btn" onclick="javascript:funCount()" value="查看答案 ">
</div></form>
<script language="Javascript">
function log(str)
{
document.write("<br>"+str);
}
//生成计算表达式
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();//正确答案的表达式
var counter = 0;//答案的个数
//测试表达式是否正确
function test(expn,a,b,c,d,m1,m2,m3)
{
var exp;
var ret;
exp = genExpress(expn,a,b,c,d,m1,m2,m3);//生成计算表达式
eval("ret = "+exp);
if ( ret - 24 < 0.1 && ret - 24 >= 0 )
{
exp = exp.replace(";","");
exp = replaceAll(exp,"*","×");
exp = replaceAll(exp, "/","÷");
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();//四个数字
//接收四个输入框的数字,调用主程序
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] < 14 && n[1] < 14 && n[2] < 14 && n[3] < 14 )
{
log("<font size=5><b>"+n[0]+", "+n[1]+", "+n[2]+", "+n[3]+"的24点答案:</b></font><br>");
log("<input type=''button'' ><br>");
funMain();
if ( counter == 0 )
{
log("没有答案!");
}
}
else
{
alert("输入错误!");
}
}
//主程序
function funMain()
{
var m = new Array();
//四种运算符
m[0] = "+";
m[1] = "-";
m[2] = "*";
m[3] = "/";
//11种表达式
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;//四个数字
var m1,m2,m3;//三个运算符
for (var i=0;i<4;i++)
{
a = n[i];
for (var j=0;j<4;j++)
{
if ( i == j )j++;//从未选的三个数字中选择一个数字
if(j>3)break;
b = n[j];
for (var x=0;x<4;x++)
{
if(i>j){max=i;min=j}else{max=j;min=i}
if(x==min)x++;
if (x==max)x++;//从未选的两个数字中选择一个数字
if(x>3)break;
c = n[x];
for (var y=0;y<4;y++)
{
if(i>j){if(x>i){max=x;mid=i;min=j}else{if(x>j){max=i;mid=x;min=j}else{max=i;mid=j;min=x}}}
else{if(x>j){max=x;mid=j;min=i}else{if(x>i){max=j;mid=x;min=i}else{max=j;mid=i;min=x}}}
if(min==y)y++;
if(mid==y)y++;
if ( max == y ) y++;//从未选的一个数字中选择一个数字
if(y>3)break;
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<12;k++)
{
eval("test(exp"+k+",a,b,c,d,m1,m2,m3);");
}
}
}
}
}
}
}
}
}
</script>
</body>
</html>