大笨狼的算法
<INPUT id=t1 value=5><br>
<INPUT id=t2 value=5><br>
<INPUT id=t3 value=5><br>
<INPUT id=t4 value=1>
<p id=p></p>
<button οnclick=vbs:SuperDullWolf id=button1 name=button1>笨狼24点牌DHTML递归算法</button>
<SCRIPT LANGUAGE=vbscript >
dim a(4),f(4),last
'计算4个数字的全排列
sub SuperDullWolf
dim count:count=0'计算无解的次数
p.innerHTML=""
for i1 =1 to 4
for i2 =1 to 4
for i3 =1 to 4
for i4 =1 to 4
if not(i1=i2 or i1=i3 or i1=i4 or i2=i3 or i2=i4 or i3=i4) then
a(1)=cint(eval("t" & i1 & ".value"))
a(2)=cint(eval("t" & i2 & ".value"))
a(3)=cint(eval("t" & i3 & ".value"))
a(4)=cint(eval("t" & i4 & ".value"))
if Iis24(a,4) then '如果找到答案就退出
exit sub
else
count=count+1'如果找不到答案继续尝试其他组合
end if
end if
Next
Next
Next
Next
if count>=24 then msgbox count & "无解!"
end sub
function sum(x,y,z)
on error resume next
sum=x
select case z
case 1
sum=sum+y
case 2
sum=sum-y
case 3
sum=sum*y
case 4
sum=sum/y
end select
last=x
end function
function showsum(x,y,z)
'显示
showsum=cstr(x)
select case z
case 1
showsum= showsum & "+" & y
case 2
showsum=showsum & "-" & y
case 3
showsum=showsum & "*" & y
case 4
showsum=showsum & "/" & y
end select
end function
function Iis24(ByRef a(),ByRef i)
if i=2 then
'如果剩下两个数直接判断
n1=a(1)
n2=a(2)'读入这两个数
for ff=1 to 4'遍历加减乘除
if sum(n1,n2,ff)=24 then
'输出结果
p.innerHTML=p.innerHTML & showsum(n1,n2,ff) & "=" & cstr(sum(n1,n2,ff)) & "<br>"
Iis24=true
exit function
else
if sum(n2,n1,ff)=24 then
'输出结果
p.innerHTML=p.innerHTML & showsum(n2,n1,ff) & "=" & cstr(sum(n2,n1,ff)) & "<br>"
Iis24=true
exit function
end if
end if
next
else
'如果有i个数,先去出两个来进行加减乘除
dim b(4),c(4)
for ii=1 to i
'取第一个数
n1=a(ii)
for jj=1 to i
if jj<>ii then
n2=a(jj)
'剩余放入数组C
nb=2
for kk=1 to i
if kk<>ii and kk<>jj then
c(nb)=a(kk)
nb=nb+1
end if
next
end if
next
for ff=1 to 4'便历加减乘除
c(1)=sum(n2,n1,ff) '取出两个数放到c中
if Iis24(c,i-1) then'递归
if sum(n2,n1,ff)=last then
p.innerHTML=p.innerHTML & showsum(n1,n2,ff) & "=" & sum(n1,n2,ff) & "<br>"
else
p.innerHTML=p.innerHTML & showsum(n2,n1,ff) & "=" & sum(n2,n1,ff) & "<br>"
end if
Iis24=true
exit function
end if
next
next
end if
end function
</SCRIPT>
24的另一种算法(忘记了作者姓名,希望作者能在看到此文章后留名)
<html>
<head>
<title>24点</title>
<meta name="author" content="dlpower@163.com">
<meta http-equiv="Content-Type" content="text/html; charset=GB2312">
</head>
<body>
<br>
计算24点(加、减、乘、除、括号) Javascript版 v1.0<br><br>
作者:<a href='mailto:dlpower@163.com'>dlpower@163.com</a> (转贴请注明作者)<br><br>
2004-04-26 <br><br><br><br>
<form>
请输入四个数字(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" οnclick="javascript:funCount()" value=" 计算 ">
</form>
<br><br>
<br><br>
一副牌(52张)中,任意抽取4张可有1820种不同组合,其中有458个牌组算不出24点。
<br><br>
二十世纪80年代中期,孙士杰从上海到美国定居。孙士杰有几位邻居是美国人。邻居家<br>
的小孩到他家串门,他没有别的玩具供孩子们玩,就灵机一动,拿出一副扑克来教他们<br>
玩“24点”。谁知,这一玩,竟令美国孩子着了迷。这些孩子又把这种游戏带回家中和<br>
学校,人们发现这种游戏对开发智力十分有益,后来就在全美推广开了。
<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' οnclick='history.back(-1);' value=' 再来一次 '><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 ) break;//从未选的三个数字中选择一个数字
b = n[j];
for (var x=0;x<4;x++)
{
if ( j == x ) break;//从未选的两个数字中选择一个数字
c = n[x];
for (var y=0;y<4;y++)
{
if ( x == y ) 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<11;k++)
{
eval("test(exp"+k+",a,b,c,d,m1,m2,m3);");
}
}
}
}
}
}
}
}
}
</script>
</body>
</html>