24点终结者(javascript)
2007-11-27 18:01:40 来源:WEB开发网源码
<SCRIPT LANGUAGE=javaScript >
function tdisoper(f0,f1,f2,f3)
{
this[0]=f0;
this[1]=f1;
this[2]=f2;
this[3]=f3;
}
disoper=new tdisoper(-,+,/,*);
function oper(f,m,n)
{
if (f==3) return(m*n);
if (f==2) return(m/n);
if (f==1) return(parseFloat(m)+parseFloat(n));
if (f==0) return(m-n);
}
function tb(i1,i2,i4,i8)
{
this[1]=i1;
this[2]=i2;
this[4]=i4;
this[8]=i8;
}
function valid(F)
{
n=F.count1.options[F.count1.selectedIndex].value;
if (isNaN(F.a1.value)){alert(必须输入整数!); F.a1.focus(); return(false);}
if (isNaN(F.a2.value)){alert(必须输入整数!); F.a2.focus(); return(false);}
if (isNaN(F.a4.value)){alert(必须输入整数!); F.a4.focus(); return(false);}
if (isNaN(F.a8.value)){alert(必须输入整数!); F.a8.focus(); return(false);}
b=new tb(F.a1.value,F.a2.value,F.a4.value,F.a8.value);
k=0;
F.text.value=;
for (i1=1;i1<=8;i1*=2)
for (i2=1;i2<=8;i2*=2)
for (i3=1;i3<=8;i3*=2)
for (i4=1;i4<=8;i4*=2)
{
if ((i1|i2|i3|i4)!=0xf) continue;
for (f1=0;f1<=3;f1++)
for (f2=0;f2<=3;f2++)
for (f3=0;f3<=3;f3++)
{
m=oper(f3,oper(f2,oper(f1,b[i1],b[i2]) ,b[i3] ) ,b[i4]);
if (Math.abs(m-24)<1e-5 ) {
F.text.value=F.text.value+((+b[i1]+disoper[f1]+b[i2]+)+disoper[f2]+b[i3]+)+disoper[f3]+b[i4]+\n;
if ((n!=0)&&(++k>=n)) return(false);
}
m=oper(f1, b[i1], oper(f3, oper(f2,b[i2],b[i3]) ,b[i4]) );
if (Math.abs(m-24)<1e-5){
F.text.value=F.text.value+b[i1]+disoper[f1]+((+b[i2]+disoper[f2]+b[i3]+)+disoper[f3]+b[i4]+)\n;
if ((n!=0)&&(++k>=n)) return(false);
}
m=oper(f3,oper(f1,b[i1], oper(f2,b[i2],b[i3]) ),b[i4]);
if (Math.abs(m-24)<1e-5){
F.text.value=F.text.value+(+b[i1]+disoper[f1]+(+b[i2]+disoper[f2]+b[i3]+))+disoper[f3]+b[i4]+\n;
if ((n!=0)&&(++k>=n)) return(false);
}
m=oper(f1, b[i1], oper(f2, b[i2], oper(f3, b[i3], b[i4]) ) );
if (Math.abs(m-24)<1e-5){
F.text.value=F.text.value+b[i1]+disoper[f1]+(+b[i2]+disoper[f2]+(+b[i3]+disoper[f3]+b[i4]+))\n;
if ((n!=0)&&(++k>=n)) return(false);
}
m=oper(f2,oper(f1,b[i1],b[i2]), oper(f3,b[i3],b[i4]) );
if (Math.abs(m-24)<1e-5){
F.text.value=F.text.value+(+b[i1]+disoper[f1]+b[i2]+)+disoper[f2]+(+b[i3]+disoper[f3]+b[i4]+)\n;
if ((n!=0)&&(++k>=n)) return(false);
}
}
}
F.text.value=F.text.value+----END----\n
return(false);
}
function newin(s,target,ww,hh)
{ window.open(s,target,menubar=0,resizable=1,scrollbars=1,width=+ww+,height=+hh)}
</SCRIPT>
<FORM onsubmit=return valid(this)>
请输入四个整数:<BR>
<INPUT TYPE=text NAME=a1 MAXLENGTH=6 size=5>
<INPUT TYPE=text NAME=a2 MAXLENGTH=6 size=5>
<INPUT TYPE=text NAME=a4 MAXLENGTH=6 size=5>
<INPUT TYPE=text NAME=a8 MAXLENGTH=6 size=5>
<SELECT NAME=count1 >
<OPTION VALUE=1 selected=on> 只找一个答案
<OPTION VALUE=5 > 最多找5个
<OPTION VALUE=10 > 最多找10个
<OPTION VALUE=0 > 找全部答案
</select>
<BR>
<INPUT TYPE=submit VALUE=计算24点><BR>
<HR>
<TEXTAREA NAME=text ROWS=15 COLS=30></TEXTAREA>
</FORM>
<HR>
<FORM><INPUT TYPE=button VALUE=返回 onClick=history.back()></FORM>
更多精彩
赞助商链接