日期:2014-05-20  浏览次数:21028 次

根据四个数字计算24点
1-10中选4个,可重复的

------解决方案--------------------
1 随机抽取4个数字
2 4个数字四则运算穷举 答案为24就 输出
应该比较麻烦
------解决方案--------------------
得到4个数字后a b c d
全排列4个数字
a a a a
a a a b
a a a c ......

全排列4个运算符
...
都有4×4×4×4种排列
然后把他们隔位组合成一个字符串表达式
难点就是 表达式求值了

方法比较笨了 呵呵
------解决方案--------------------
像咱笨人...只能穷举了...穷举也不是很多种情况哈

0±a±b±c±d 16种

1*/(±a±b±c)*/(±d) 4*64=256种

1*/(±a±b)*/(±c±d) 2*64=128种

1*/(±a)*/(±b)*/(±c)*/(±d) 256种

不加开方啥的应该够了...656种可能性
------解决方案--------------------
这个是用JS写的24点程序,你把它保存复制保存为html就可以了.(例如5,5,5,1.也可以算)
<%@ page contentType= "text/html;charset=gbk " %>
<html>
<head>
<title> 算24点程序 </title>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
</head>

<tr> <td align= "center ">
<form method= "post " action= "# " id= "form1 ">
<input style= "width:35 " id= "t1 " name= "t1 ">
<input style= "width:35 " id= "t2 " name= "t2 ">
<input style= "width:35 " id= "t3 " name= "t3 ">
<input style= "width:35 " id= "t4 " name= "t4 "> <br>
<input style= "width:115 " id= "t5 " name= "t5 ">
<input type=reset name= "t6 " value= "清除 ">
</form>
<script language=javascript>
function fun1(e1,e2,e3,e4)
{
var i,j,k,a,b,c,d,t1,t2,t3;
var opx=[ "+ ", "- ", "* ", "/ ", "- ", "/ "];
a=e1;b=e2;c=e3;d=e4;
for(i=0;;)
{
if(i> =4)break;
if(i==0)t1=a+b;
if(i==1)if(a-b> 0)t1=a-b;
if(i==2)t1=a*b;
if(i==3)if(b!=0)t1=a/b;
for(j=0;;)
{
if(j> 5)break;
if(j==0)t2=t1+c;
if(j==1)t2=t1-c;
if(j==2)t2=t1*c;
if(j==3)if(c!=0)t2=t1/c;
if(j==4)t2=c-t1;
if(j==5)if(t1!=0)t2=c/t1;
for(k=0;;k++)
{
if(k> 5)break;
if(k==0)t3=t2+d;
if(k==1)t3=t2-d;
if(k==2)t3=t2*d;
if(k==3)if(d!=0)t3=t2/d;
if(k==4)t3=d-t2;
if(k==5)if(t2!=0)t2=d/t2;
if(t3==24)
{var str= " ";
var t= " ";
/*if(j> 1||k> 1)document.write ( "( ");
document.write (a+opx[i]+b);
if(j> 1)document.write ( ") ");
document.write (opx[j]+c);
if(j <2&&k> 1)document.write ( ") ");
document.write (opx[k]+d);
document.write ( "=24 <br> ");*/

if((j> 1||k> 1)&&i <2)t= "( ";
if((j <2||j==4)&&k> 1&&i> 1)t= "( ";
str=str+a+opx[i]+b;
if(j> 1&&i <2)str=str+ ") ";
if(j <4)str=str+opx[j]+c;
else if(j> 3&&i <2)str=c+opx[j]+ "( "+str+ ") ";
else if(j==5&&i==3)str=c+opx[j]+ "( "+str+ ") ";