相当棒的24点游戏算法,Javascript写的,不懂,谁能翻译成C/C++?

塘外人 2008-11-21 10:53:30
加精
24点算法

http://www.prcer.com/tools/24/24-1.html
<script language="JavaScript">

// 程序作者:海浪
// 日期:2005-8-13

function lop(j)
{
if(j>0)
{
var ss = lop(j-1);
var px = "";
var oo = "$1"+j+"$2";
for(var k=0; k<=j; k++)
{
var re = new RegExp("(\\d{"+k+"})(.*?;)","g");
px += ss.replace(re,oo);
}
return px;
}
else
return "0;";
}

var gt = lop(3).slice(0,-1).split(";");
for(var i=0; i<gt.length; i++)
gt[i] = gt[i].split("");

var ot = [];
var op = ["+","-","*","/"];
for(var x=0; x<4; x++)
for(var y=0; y<4; y++)
for(var z=0; z<4; z++)
{
ot[ot.length] = "( (a"+op[x]+"b)"+op[y]+"c) "+op[z]+"d";
ot[ot.length] = "( a"+op[x]+"(b"+op[y]+"c)) "+op[z]+"d";
ot[ot.length] = "a"+op[x]+"( (b"+op[y]+"c)"+op[z]+"d) ";
ot[ot.length] = "a"+op[x]+"( b"+op[y]+"(c"+op[z]+"d)) ";
ot[ot.length] = "( a"+op[x]+"b) "+op[y]+"(c"+op[z]+"d)";
}

///////////////////////////////////////////////////////////////

var ggcs;

function ggcsclass()
{
this.pjys = 0;
this.zq = 0;
this.cw = 0;
this.hl = 0;
this.gx();
}

function ggcsclass.prototype.gx()
{
k_pjys.innerText = this.zq==0?0:Math.round(this.pjys/this.zq*100)/100;
k_zq.innerText = this.zq;
k_cw.innerText = this.cw;
k_hl.innerText = this.hl;

}

function ggcsclass.prototype.phl()
{
this.hl++;
this.gx();
k_jg.innerText = "此题被忽略!!";
}

function ggcsclass.prototype.pcw()
{
this.cw++;
this.gx();
k_jg.innerText = "错误!!";
}

function ggcsclass.prototype.pzq()
{
var d = jcsl.endDate();
this.pjys += d/1000;
this.zq++;
this.gx();
k_jg.innerText = "正确!用时"+Math.round(d/10)/100+"秒!";
}


window.onload = function()
{
ggcs = new ggcsclass();
}

///////////////////////////////////////////////////////////////

var jcsl=null;

function jclass()
{
this.xpa = [];
this.ssjg = "";
this.wx = 1E-10;
this.date = null;
}

function jclass.prototype.rnd()
{
for(var ii=0; ii<4; ii++)
this.xpa[ii] = Math.floor(Math.random()*10)+1;
}

function jclass.prototype.fapai()
{
for(var ii=0; ii<4; ii++)
document.getElementById("pk"+ii).innerText = this.xpa[ii];
this.date = new Date();
}

function jclass.prototype.endDate()
{
return new Date()-this.date;
}

function jclass.prototype.jw()
{
var a,b,c,d;
this.ssjg= "";
for(var i=0; i<gt.length; i++)
{
a=this.xpa[gt[i][0]]-0;
b=this.xpa[gt[i][1]]-0;
c=this.xpa[gt[i][2]]-0;
d=this.xpa[gt[i][3]]-0;
for(var h=0; h<ot.length; h++)
if(Math.abs(eval(ot[h])-24)<=this.wx)
this.jcf(ot[h].replace("a",a).replace("b",b).replace("c",c).replace("d",d));
}
}

function jclass.prototype.jcf(t)
{
var str = t;
var zz1 = t.replace(/[\(\)]/g,"");
var zz2 = t.replace(/[\(\)] /g,"");
var zz3 = t.replace(/[\(\)](?! )/g,"");
if(Math.abs(eval(zz1)-24)<=this.wx)
str = zz1;
else if(Math.abs(eval(zz2)-24)<=this.wx)
str = zz2;
else if(Math.abs(eval(zz3)-24)<=this.wx)
str = zz3;
var res = str.replace(/ /g,"")+"=24\n";
if(this.ssjg.indexOf(res)==-1)
this.ssjg += res;

}

function jclass.prototype.scda()
{
sskgxx.innerText = "全部答案:\n"+this.ssjg;
}

function jclass.prototype.gx(ss)
{
var sw1 = ss.match(/\(/g), sw2 = ss.match(/\)/g);
sw1 = (sw1==null)?0:sw1.length;
sw2 = (sw2==null)?0:sw2.length;
if(sw1!=sw2)
return "左右括号个数不一样";
if(!/^\(*(\d+)\)*[\+\-\*\/]\(*(\d+)\)*[\+\-\*\/]\(*(\d+)\)*[\+\-\*\/]\(*(\d+)\)*$/.test(ss))
return "算术表达式格式不正确";
var str = "#"+RegExp.$1+"|#"+RegExp.$2+"|#"+RegExp.$3+"|#"+RegExp.$4+"|";
for(var ii=0; ii<4; ii++)
{
var lok = "#"+this.xpa[ii]+"|";
if(str.indexOf(lok)!=-1)
str = str.replace(lok,"");
else
return "算术表达式中数字不对";
}
return "yes";
}


//////////////////////////////////////////////////////////////



function fpaaSystem()
{
if(jcsl!=null)
ggcs.phl();
jcsl = new jclass();
do {
jcsl.rnd();
jcsl.jw();
} while(jcsl.ssjg=="");
k_jg.innerText = "";
sskgxx.innerText = "";
gstext.value = "";
jcsl.fapai();
}

function fpaaInput()
{
var ins = prompt("请输入四个数字(如: 5,4,7,10)","");
if(ins==null) return;
if(!/^\d{1,2},\d{1,2},\d{1,2},\d{1,2}$/.test(ins)) { alert("输入的内容不正确!");return; }
var vf = jcsl!=null;
jcsl = new jclass();
jcsl.xpa = ins.split(",");
jcsl.jw();
if(jcsl.ssjg=="") { alert("此题无解!!!");return; }

if(vf)
ggcs.phl();
k_jg.innerText = "";
sskgxx.innerText = "";
gstext.value = "";
jcsl.fapai();
}


function ckda()
{
if(jcsl==null) return;
ggcs.phl();
jcsl.scda();
jcsl=null;
}

function okaa()
{
if(jcsl==null) return;
var gs = gstext.value;
var str = jcsl.gx(gs);
if(str!="yes")
{
alert("算式错误:"+str+"!");
return;
}
if(Math.abs(eval(gs)-24)<=jcsl.wx)
ggcs.pzq();
else
ggcs.pcw();
jcsl.scda();
jcsl=null;
}



function tocs(n)
{
if(jcsl==null) return;
gstext.value+=jcsl.xpa[n];
}


</script>

谁能翻译下呢?
...全文
4611 150 打赏 收藏 转发到动态 举报
写回复
用AI写文章
150 条回复
切换为时间正序
请发表友善的回复…
发表回复
qjzsj 2012-01-28
  • 打赏
  • 举报
回复
flash版的24点游戏
http://app.baidu.com/widget?appid=123723
xxmeng2008 2010-09-01
  • 打赏
  • 举报
回复
正做24点算法的课设呢 来学习一下
yeelone 2010-08-30
  • 打赏
  • 举报
回复
先回帖,再留言
saltprune 2009-09-14
  • 打赏
  • 举报
回复
太长了,有空来看看。
renzaijiang 2009-09-11
  • 打赏
  • 举报
回复
#i nclude <iostream>
#i nclude <string>
#i nclude <cmath>
using namespace std;
const double P = 1E-6;
const int Count = 4;
double numbers[Count];
string OutWord[Count];
bool JiSuan(int n)
{

if (n == 1)
{
if ( fabs(numbers[0] - 24) < P )
{
cout << OutWord[0] << endl; //输出表达式
return true;
}
else
{
return false;
}
}

for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
double numa, numb;
string stra, strb;
numa = numbers[i];
numb = numbers[j];
numbers[j] = numbers[n - 1];
stra = OutWord[i];
strb = OutWord[j];
OutWord[j] = OutWord[n - 1];
OutWord[i] = '(' + stra + '+' + strb + ')';
numbers[i] = numa + numb;
if ( JiSuan(n - 1) )
return true;
OutWord[i] = '(' + stra + '-' + strb + ')';
numbers[i] = numa - numb;
if ( JiSuan(n - 1) )
return true;
OutWord[i] = '(' + strb + '-' + stra + ')';
numbers[i] = numb - numa;
if ( JiSuan(n - 1) )
return true;
OutWord[i] = '(' + stra + '*' + strb + ')';
numbers[i] = numa * numb;
if ( JiSuan(n - 1) )
return true;
if (numb != 0)
{
OutWord[i] = '(' + stra + '/' + strb + ')';
numbers[i] = numa / numb;
if ( JiSuan(n - 1) )
return true;
}
if (numa != 0)
{
OutWord[i] = '(' + strb + '/' + stra + ')';
numbers[i] = numb / numa;
if ( JiSuan(n - 1) )
return true;
}
numbers[i] = numa;
numbers[j] = numb;
OutWord[i] = stra;
OutWord[j] = strb;
}
}
return false;
}


void main()
{
char choice;
START:
cout<<"输入四个整数:";
for (int i = 0; i < Count; i++)
{
char f[20];
int x;
cin >> x;
numbers[i] = x;
itoa(x, f, 10);
OutWord[i] = f;
}
if ( JiSuan(Count) )
{
cout << "符合要求" << endl;
}
else
{
cout << "不符合要求" << endl;
}
fflush(stdin);
printf("按 'Y' 键再来一次:");
choice=getchar();
if(choice=='Y'||choice=='y')
{
printf("\n",choice);
goto START;
}
}
这是一个递归算法 应该还蛮好懂
renzaijiang 2009-09-11
  • 打赏
  • 举报
回复
#i nclude <iostream>
#i nclude <string>
#i nclude <cmath>
using namespace std;
const double P = 1E-6;
const int Count = 4;
double numbers[Count];
string OutWord[Count];
bool JiSuan(int n)
{

if (n == 1)
{
if ( fabs(numbers[0] - 24) < P )
{
cout << OutWord[0] << endl; //输出表达式
return true;
}
else
{
return false;
}
}

for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
double numa, numb;
string stra, strb;
numa = numbers[i];
numb = numbers[j];
numbers[j] = numbers[n - 1];
stra = OutWord[i];
strb = OutWord[j];
OutWord[j] = OutWord[n - 1];
OutWord[i] = '(' + stra + '+' + strb + ')';
numbers[i] = numa + numb;
if ( JiSuan(n - 1) )
return true;
OutWord[i] = '(' + stra + '-' + strb + ')';
numbers[i] = numa - numb;
if ( JiSuan(n - 1) )
return true;
OutWord[i] = '(' + strb + '-' + stra + ')';
numbers[i] = numb - numa;
if ( JiSuan(n - 1) )
return true;
OutWord[i] = '(' + stra + '*' + strb + ')';
numbers[i] = numa * numb;
if ( JiSuan(n - 1) )
return true;
if (numb != 0)
{
OutWord[i] = '(' + stra + '/' + strb + ')';
numbers[i] = numa / numb;
if ( JiSuan(n - 1) )
return true;
}
if (numa != 0)
{
OutWord[i] = '(' + strb + '/' + stra + ')';
numbers[i] = numb / numa;
if ( JiSuan(n - 1) )
return true;
}
numbers[i] = numa;
numbers[j] = numb;
OutWord[i] = stra;
OutWord[j] = strb;
}
}
return false;
}


void main()
{
char choice;
START:
cout<<"输入四个整数:";
for (int i = 0; i < Count; i++)
{
char f[20];
int x;
cin >> x;
numbers[i] = x;
itoa(x, f, 10);
OutWord[i] = f;
}
if ( JiSuan(Count) )
{
cout << "符合要求" << endl;
}
else
{
cout << "不符合要求" << endl;
}
fflush(stdin);
printf("按 'Y' 键再来一次:");
choice=getchar();
if(choice=='Y'||choice=='y')
{
printf("\n",choice);
goto START;
}
}
这个我原来在网上收的 应该蛮好懂
FANGHAONO1 2009-04-09
  • 打赏
  • 举报
回复
mark
塘外人 2008-12-16
  • 打赏
  • 举报
回复
沉了。。。。。。。。。。
塘外人 2008-12-16
  • 打赏
  • 举报
回复
沉了。。。。。。。。。。
dj4156003 2008-12-04
  • 打赏
  • 举报
回复
[Quote=引用 116 楼 yhmhappy2006 的回复:]
引用 24 楼 dj4156003 的回复:
我的不就能算出所有答案么`?

那么请问怎样才能看到你的全面答案呢?通过什么命令?
[/Quote]

如果单纯的只是计算24点的解的话那肯定一个DFS就够了..
我为什么写了那么多`?因为我里面还写了一个表达式计算的函数..
如果不写那对于对于用户输入的表达式我怎么计算呢`?
我想这个表达式的计算应该大家都知道吧`?学栈的时候一个最经典的算法.我觉得这不算复杂吧`?
你想要看所有的解很简单.对于电脑给出的数你给出一个错误的表达式..就可以查看所有的正确答案了..
而且我这个程序中电脑给出的四个数是绝对有解的.因为如果随机到无解的情况那我的程序会去掉自动生成下一组四个数.
如果你想计算任意的四个数.那更简单.你把我程序里随机生成四个数那段去掉.然后把OperateNumA数组中的数改为用户输入就行了..
我程序里面icp,isp,isnum,GetOperands,DoOperator,run,f这几个函数都是用来计算表达式值的.
DFS是用来算24点的.如果你不需要算用户提供的表达式的值那你只保留DFS就行了..
塘外人 2008-12-04
  • 打赏
  • 举报
回复
楼上解释的真详细,多谢啦
我研究研究。。。。
塘外人 2008-12-02
  • 打赏
  • 举报
回复
[Quote=引用 134 楼 webtonny 的回复:]
有一本书里面讨论了关于24点的三种算法,书名叫编程艺术好象,是微软亚洲研究院出的.
[/Quote]
好的,去翻翻看。。。。。
webtonny 2008-12-02
  • 打赏
  • 举报
回复
有一本书里面讨论了关于24点的三种算法,书名叫编程艺术好象,是微软亚洲研究院出的.
j9988 2008-12-02
  • 打赏
  • 举报
回复
24点找出答案不是水平,去除重复答案。才是真水准。
j9988 2008-12-02
  • 打赏
  • 举报
回复
一般,答案重复。我见过更强的,一个重复答案都没。而且在CSDN上。
j9988 2008-12-02
  • 打赏
  • 举报
回复
一般,答案重复。我见过更强的,一个重复答案都没。而且在CSDN上。
恋空 2008-12-02
  • 打赏
  • 举报
回复
[Quote=引用 128 楼 Jack_Yin 的回复:]
引用 12 楼 myvicy 的回复:
我再发一个值得收藏的,呵呵。

<script>
function get24(n1,n2,n3,n4){
(s=String.prototype).r=s.replace;q:for(i in a=[n1,n2,n3,n4])for(j in a)for(k in a)for(l in a)if(i!=j&&i!=k&&i!=l&&j!=k&&j!=l&&k!=l)for(m in b=["(x+y)","(x-y)","(x*y)","(x/y)","(y-x)","(y/x)"])for(n in b)for(o in b)if((d=Math.abs)(eval(s=b[o].r(x="x",b[n].r(x,p=b[m].r(x,a[i]).r(y="y",a[j])).r(y,g=a[…
[/Quote]都是牛人,我是新人。。。
HuanxueOrSeaty 2008-12-02
  • 打赏
  • 举报
回复
jeff_nie 2008-12-01
  • 打赏
  • 举报
回复
MARK
Jack_Yin 2008-12-01
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 myvicy 的回复:]
我再发一个值得收藏的,呵呵。

<script>
function get24(n1,n2,n3,n4){
(s=String.prototype).r=s.replace;q:for(i in a=[n1,n2,n3,n4])for(j in a)for(k in a)for(l in a)if(i!=j&&i!=k&&i!=l&&j!=k&&j!=l&&k!=l)for(m in b=["(x+y)","(x-y)","(x*y)","(x/y)","(y-x)","(y/x)"])for(n in b)for(o in b)if((d=Math.abs)(eval(s=b[o].r(x="x",b[n].r(x,p=b[m].r(x,a[i]).r(y="y",a[j])).r(y,g=a[k])).r(y,h=a[l]))-24) <0…
[/Quote]
好神奇
加载更多回复(125)

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧