体验一下,代码如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function js()
{
if(!abc())
{
document.getElementById("test").innerText = "无解!";
}
}
function abc()
{
var x = new Array();
x[0] = document.getElementById("a").value;
x[1] = document.getElementById("b").value;
x[2] = document.getElementById("c").value;
x[3] = document.getElementById("d").value;
var a,b,c,d,abN,abcN,abcdN,str,cnt,a1,a2,a3,N;
str = "";
cnt = 0;
abN = 0;
abcN = 0;
abcdN = 0;
N = 0;
for(var i=0;i<4;i++)
{
a = x[i];
for(var j=0;j<4;j++)
{
b = x[j];
if(j == i)continue;
for(var m=0;m<4;m++)
{
c = x[m];
if(m == i || m == j)continue;
for(var n=0;n<4;n++)
{
d = x[n];
if(n == i || n == j || n ==m)continue;
for(var o=0;o<6;o++)
{
abN = Com(a,b,o);
a1 = ComF(a,b,o);
for(var p=0;p<6;p++)
{
abcN = Com(abN,c,p);
a2 = ComF(abN,c,p);
for(var q=0;q<6;q++)
{
abcdN = Com(abcN,d,q);
a3 = ComF(abcN,d,q);
N = N + 1;
if(abcdN==24)
{
document.getElementById("test").innerText = a1 + "\n" + a2 + "\n" + a3 + "\n运算次数:"+N;
return true;
}
}
}
}
}
}
}
}
return false;
}
function Com(a,b,OO)
{
switch (OO){
case 0:
return parseFloat(a) + parseFloat(b);
case 1:
return parseFloat(a) - parseFloat(b);
case 2:
return parseFloat(a) * parseFloat(b);
case 3:
return parseFloat(a) / parseFloat(b);
case 4:
return parseFloat(b) - parseFloat(a);
case 5:
return parseFloat(b) / parseFloat(a);
}
}
function ComF(a,b,OO)
{
switch (OO){
case 0:
return a + " + " + b + " = " + (parseFloat(a)+parseFloat(b));
case 1:
return a + " - " + b + " = " + (parseFloat(a)-parseFloat(b));
case 2:
return a + " * " + b + " = " + (parseFloat(a)*parseFloat(b));
case 3:
return a + " / " + b + " = " + (parseFloat(a)/parseFloat(b));
case 4:
return b + " - " + a + " = " + (parseFloat(b)-parseFloat(a));
case 5:
return b + " / " + a + " = " + (parseFloat(b)/parseFloat(a));
}
}
</script>
</head>
<body>
<p>
<input name="a" type="text" value="1" />
<input name="b" type="text" value="2" />
<input name="c" type="text" value="3" />
<input name="d" type="text" value="4" />
</p>
<p>
<input type="button" name="Submit" value="按钮" onclick="js()" />
</p>
<div id=test></div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<script language="javascript">
function js()
{
if(!abc())
{
document.getElementById("test").innerText = "无解!";
}
}
function abc()
{
var x = new Array();
x[0] = document.getElementById("a").value;
x[1] = document.getElementById("b").value;
x[2] = document.getElementById("c").value;
x[3] = document.getElementById("d").value;
var a,b,c,d,abN,abcN,abcdN,str,cnt,a1,a2,a3,N;
str = "";
cnt = 0;
abN = 0;
abcN = 0;
abcdN = 0;
N = 0;
for(var i=0;i<4;i++)
{
a = x[i];
for(var j=0;j<4;j++)
{
b = x[j];
if(j == i)continue;
for(var m=0;m<4;m++)
{
c = x[m];
if(m == i || m == j)continue;
for(var n=0;n<4;n++)
{
d = x[n];
if(n == i || n == j || n ==m)continue;
for(var o=0;o<6;o++)
{
abN = Com(a,b,o);
a1 = ComF(a,b,o);
for(var p=0;p<6;p++)
{
abcN = Com(abN,c,p);
a2 = ComF(abN,c,p);
for(var q=0;q<6;q++)
{
abcdN = Com(abcN,d,q);
a3 = ComF(abcN,d,q);
N = N + 1;
if(abcdN==24)
{
document.getElementById("test").innerText = a1 + "\n" + a2 + "\n" + a3 + "\n运算次数:"+N;
return true;
}
}
}
}
}
}
}
}
return false;
}
function Com(a,b,OO)
{
switch (OO){
case 0:
return parseFloat(a) + parseFloat(b);
case 1:
return parseFloat(a) - parseFloat(b);
case 2:
return parseFloat(a) * parseFloat(b);
case 3:
return parseFloat(a) / parseFloat(b);
case 4:
return parseFloat(b) - parseFloat(a);
case 5:
return parseFloat(b) / parseFloat(a);
}
}
function ComF(a,b,OO)
{
switch (OO){
case 0:
return a + " + " + b + " = " + (parseFloat(a)+parseFloat(b));
case 1:
return a + " - " + b + " = " + (parseFloat(a)-parseFloat(b));
case 2:
return a + " * " + b + " = " + (parseFloat(a)*parseFloat(b));
case 3:
return a + " / " + b + " = " + (parseFloat(a)/parseFloat(b));
case 4:
return b + " - " + a + " = " + (parseFloat(b)-parseFloat(a));
case 5:
return b + " / " + a + " = " + (parseFloat(b)/parseFloat(a));
}
}
</script>
</head>
<body>
<p>
<input name="a" type="text" value="1" />
<input name="b" type="text" value="2" />
<input name="c" type="text" value="3" />
<input name="d" type="text" value="4" />
</p>
<p>
<input type="button" name="Submit" value="按钮" onclick="js()" />
</p>
<div id=test></div>
</body>
</html>