24點演算法有問題

來源:互聯網
上載者:User
演算法|問題|演算法 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);&qu



相關文章

Cloud Intelligence Leading the Digital Future

Alibaba Cloud ACtivate Online Conference, Nov. 20th & 21st, 2019 (UTC+08)

Register Now >

Starter Package

SSD Cloud server and data transfer for only $2.50 a month

Get Started >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。