计算24点的程序讨论,欢迎大家进来

smq 2007-04-28 10:10:20
http://club.it.sohu.com/read_art_sub.new.php?b=program&a=73842&sr=0&allchildnum=1&t=0&NoCache=1

欢迎大家发表看法,谢谢帮顶,会给分的
...全文
305 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
up
fengdream 2007-04-29
  • 打赏
  • 举报
回复
收藏了
linux_is_perfect 2007-04-29
  • 打赏
  • 举报
回复
不错
llyzcy 2007-04-28
  • 打赏
  • 举报
回复
up
contain_universe 2007-04-28
  • 打赏
  • 举报
回复
get scores...
lidongri 2007-04-28
  • 打赏
  • 举报
回复
不难
yoyo_alex_lw 2007-04-28
  • 打赏
  • 举报
回复
网上大把的,帮顶了。。。。
celftj 2007-04-28
  • 打赏
  • 举报
回复
JF素最重要的
mdejtod 2007-04-28
  • 打赏
  • 举报
回复
收藏
bargio_susie 2007-04-28
  • 打赏
  • 举报
回复
网上大把的,帮顶了。。。。
bargio_susie 2007-04-28
  • 打赏
  • 举报
回复
SF
王集鹄 2007-04-28
  • 打赏
  • 举报
回复
//这是算法版大牛海星写的算法,我翻译成C#的

private const double Precision = 1E-6; // 精度
private bool fSearchExpression(double[] ANumbers, string[] AExpressions,
int ALevel, int ADest, List<string> AResults)
{
bool Result = false;
if ((ALevel <= 1) && (Math.Abs(ANumbers[0] - ADest) <= Precision))
{
AResults.Add(AExpressions[0]);
return true;
}
for (int i = 0; i < ALevel; i++)
for (int j = i + 1; j < ALevel; j++)
{
double A = ANumbers[i];
double B = ANumbers[j];
ANumbers[j] = ANumbers[ALevel - 1];
string vExpA = AExpressions[i];
string vExpB = AExpressions[j];
AExpressions[j] = AExpressions[ALevel - 1];
AExpressions[i] = '(' + vExpA + '+' + vExpB + ')';
ANumbers[i] = A + B;
if (fSearchExpression(ANumbers, AExpressions,
ALevel - 1, ADest, AResults)) Result = true;
AExpressions[i] = '(' + vExpA + '-' + vExpB + ')';
ANumbers[i] = A - B;
if (fSearchExpression(ANumbers, AExpressions,
ALevel - 1, ADest, AResults)) Result = true;
AExpressions[i] = '(' + vExpB + '-' + vExpA + ')';
ANumbers[i] = B - A;
if (fSearchExpression(ANumbers, AExpressions,
ALevel - 1, ADest, AResults)) Result = true;
AExpressions[i] = '(' + vExpA + '*' + vExpB + ')';
ANumbers[i] = A * B;
if (fSearchExpression(ANumbers, AExpressions,
ALevel - 1, ADest, AResults)) Result = true;
if (B != 0)
{
AExpressions[i] = '(' + vExpA + '/' + vExpB + ')';
ANumbers[i] = A / B;
if (fSearchExpression(ANumbers, AExpressions,
ALevel - 1, ADest, AResults)) Result = true;
}
if (A != 0)
{
AExpressions[i] = '(' + vExpB + '/' + vExpA + ')';
ANumbers[i] = B / A;
if (fSearchExpression(ANumbers, AExpressions,
ALevel - 1, ADest, AResults)) Result = true;
}
ANumbers[i] = A;
ANumbers[j] = B;
AExpressions[i] = vExpA;
AExpressions[j] = vExpB;
}
return Result;
}

private bool SearchExpression(List<string> AResults, int ADest, params int[] ANumbers)
{
double[] vNumbers = new double[ANumbers.Length];
string[] vExpressions = new string[ANumbers.Length];
for (int i = 0; i < ANumbers.Length; i++)
{
vNumbers[i] = ANumbers[i];
vExpressions[i] = ANumbers[i].ToString();
}
return fSearchExpression(vNumbers, vExpressions, ANumbers.Length, ADest, AResults);
}

private void button1_Click(object sender, EventArgs e)
{
List<string> vExpressions = new List<string>();
SearchExpression(vExpressions, 22, 1, 2, 3, 4, 5);
foreach (string vExpression in vExpressions)
textBox1.AppendText(vExpression + "\r\n");
}
AiNiLife 2007-04-28
  • 打赏
  • 举报
回复
学习学习。。
chinson 2007-04-28
  • 打赏
  • 举报
回复
先顶再说
spofmy 2007-04-28
  • 打赏
  • 举报
回复
up up
jixingzhong 2007-04-28
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

typedef float (*PF)(float, float);
char *ch[]={NULL,"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
char cc[]="+-*/";
//int rand_seed = 0;

void rand_num(int *num)
{
int i;
// srand(num[(rand_seed++)%4]);
for(i=0; i<4; i++)
num[i] = rand()%12 +1;
}

float add(float x, float y)
{
return x+y;
}

float sub(float x, float y)
{
return x-y;
}

float mul(float x, float y)
{
return x*y;
}

float divide(float x, float y)
{
if((y>-0.0000001)&&(y<0.0000001))
return 0;
else return x/y;
}


/*The compute function!*/
void compute_24(int num[])
{
PF pf[4]={add,sub,mul,divide};
int i,j,k,x,y,z,t;
int flag=0,flag_i=0, flag_j=0, flag_equ_end2=0;
float op_x, op_y, op_z;

/*Kind 1*/
for(i=0; i<4; i++) /*num[i] is the first para*/
{
for(t=0; t<i; t++)
if(num[t] == num[i])
flag_i = 1;
if(flag_i)
{
flag_i=0;
continue;
}

for(j=0; j<4; j++) /*num[j] is the 2nd para*/
{
if(j == i)
continue;

for(t=j-1; t>0; t--)
if((num[j] == num[t])&&(num[i]!=num[t]))
flag_j = 1;
if(flag_j)
{
flag_j=0;
continue;
}

for(x=0; x<4; x++) /*pf[x] is the 1st operation*/
{
if((j<i)&&
((x==0)||(x==2))
)
continue;

op_x = pf[x]((float)num[i],(float)num[j]);
for(k=0; k<4; k++) /*num[k] is the 3rd para, and the last is num[6-i-j-k]*/
{
if(k==i || k==j)
continue;

if(num[k] == num[6-i-j-k])
flag_equ_end2 = 1;

for(y=0; y<4; y++) /*pf[y] is the 2nd operation*/
{
if( (k<j)&&
(((x<2)&&(y<2)) ||
((x>1)&&(y>1)))
)
continue;

op_y = pf[y](op_x, (float)num[k]);
for(z=0; z<4; z++) /*pf[z] is the 3rd operation*/
{
if((x==y)&&(x==z)&&((i!=0)||(j!=1)||(k!=2)))
continue;

if( (((z<2)&&(y<2)) ||
((z>1)&&(y>1))) &&
(k>(6-i-j-k))
)
continue;

op_z = pf[z](op_y, (float)num[6-i-j-k]);
if(((op_z - 24)>-0.0000001) && ((op_z - 24)<0.0000001))
{
flag=1;
printf("((%d %c %d) %c %d) %c %d == 24\n",num[i], cc[x], num[j], cc[y], num[k], cc[z], num[6-i-j-k]);
}
}
}
if(flag_equ_end2)
{
flag_equ_end2 = 0;
break;
}
}
}
}
}

/*Kind 2*/
for(i=0; i<4; i++) /*num[i] is the first para*/
{
for(t=0; t<i; t++)
if(num[t] == num[i])
flag_i = 1;
if(flag_i)
{
flag_i=0;
continue;
}

for(j=0; j<4; j++) /*num[j] is the 2nd para*/
{
if(j == i)
continue;

for(t=j-1; t>-1; t--)
if((num[j] == num[t])&&(num[i]!=num[t]))
flag_j = 1;
if(flag_j)
{
flag_j=0;
continue;
}

for(x=0; x<2; x++) /*pf[x] is the 1st operation*/
{
if((j<i)&&(x==0))
continue;
op_x = pf[x]((float)num[i],(float)num[j]);
for(k=0; k<4; k++) /*num[k] is the 3rd para, and the last is num[6-i-j-k]*/
{
if(k==i || k==j)
continue;
for(y=0; y<2; y++) /*pf[y] is the 2nd operation*/
{
if((y==0)&&(k>(6-i-j-k)))
continue;
op_y = pf[y]((float)num[k],(float)num[6-i-j-k]);
for(z=2; z<4; z++) /*pf[z] is the 3rd operation*/
{
if((z==2) &&
(((k<i)&&(k<j)) ||
(((6-i-j-k)<i)&&((6-i-j-k)<j))
)
)
continue;
op_z = pf[z](op_x, op_y);
if(((op_z - 24)>-0.0000001) && ((op_z - 24)<0.0000001))
{
flag=1;
printf("(%d %c %d) %c (%d %c %d) == 24\n",num[i], cc[x], num[j], cc[z], num[k], cc[y], num[6-i-j-k]);
}
}
}
}
}
}
}
if(!flag)
{
flag = 0;
puts("Sorry, there is no answer!!");
}
}


int main()
{
int num[4],i;
char c='y';

while(c == 'y')
{
rand_num(num);

for(i=0; i<4; i++)
printf("%d--%s\t",num[i],ch[num[i]]);
puts("");
compute_24(num);
printf("\n\nEnter 'y' continue or others break: ");
scanf("%c",&c);
fflush(stdin);
}
}

分类穷举 op 运算的方法。

69,378

社区成员

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

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