一个关于“二十四点”的问题

gordon_dengcn 2002-07-14 09:14:06
大家好:
一个小问题靠靠大家:3,3,8,8,请算出24。方法越多越好!
顺便,有没有人有“二十四点游戏”算法的源程序,请告诉我下载网址。
我的网站:http://slzx.boy.net.cn 欢迎访问!
谢谢!
...全文
104 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhi_chong 2002-07-14
  • 打赏
  • 举报
回复
请问 qianli918(千里马),你的程序中的err是有什么用的???
zhi_chong 2002-07-14
  • 打赏
  • 举报
回复
用穷举法就可以了吧
qianli918 2002-07-14
  • 打赏
  • 举报
回复
wobiande

#include<stdio.h>
#include<ctype.h>
#include<math.h>
#include<string.h>
#include<iostream.h>
const char op[]="+-*/";
const float err=-0.1234;
const N=50;
int K;
int s=0;
const m=10;
char ct[m][N];
int n0;
void make(float st[],int n)
{int i,j,t;char x0[N],y0[N],z0[N];//1[
float x,y,z;
for(i=0;i<n0;i++)
if (st[i]!=err)
{x=st[i];st[i]=err;//2[
strcpy(x0,ct[i]);
for(j=0;j<n0;j++)
if(st[j]!=err)
{y=st[j];st[j]=err;strcpy(y0,ct[j]);//3[
for(t=0;t<4;t++)
{z=err; //4[
switch (op[t])
{case '+':if(x<=y)z=x+y;break;//5[
case '-':z=x-y;break;
case '*':if(x<=y)z=x*y;break;
case '/':if(fabs(y)>0.01)z=x/y;break;
} //5]
if(z!=err)
{sprintf(z0,"%s%c%s",x0,op[t],y0);//6[
if(n!=2)
{st[i]=z; //7[
sprintf(ct[i],"(%s)",z0);
make(st,n-1);
st[i]=err;strcpy(ct[i],x0);}//7]
else
if(fabs(z-K)<0.001){s=1;throw z0;}
//printf("第%d组解:%s=%d\n",++s,z0,K);
}//6]
}//4]
st[j]=y; }//3]
st[i]=x;}//2]
}//1]
void main()
{int i;float st[m];char c;
printf("此程序用n个数通过加减乘除算你所要的结果\n");
do{
do {printf("请输入n:");cin>>n0;}while (n0<2);
printf("\n请输入你所需要的结果:");cin>>K;
printf("\n请输入%d个数:\n",n0);s=0;
for(i=0;i<n0;i++)
{cin>>st[i];
sprintf(ct[i],"%d",int(st[i]));
}
try{make(st,n0);}
catch (char *z0)
{printf("%s=%d\n",z0,K);}
if(!s)printf("对不起,此题无解!\n");
printf("继续吗(y/n)?");
while((c=toupper(getchar()))!='Y'&&c!='N');
}while (c=='Y');
}
boodweb 2002-07-14
  • 打赏
  • 举报
回复
我的网站上有一个:
http://qhb.363.net
自制软件里
atlantis13579 2002-07-14
  • 打赏
  • 举报
回复
8/(3-8/3)

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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