您的位置:首页 > 其它

24点问题

2011-04-19 22:56 127 查看
这是求解24点问题的一个解法,考虑到可移植性,我只用了C的标准库函数。
程序是我根据《编程之美》里面的算法写的,使用的基本算法是递归。基本思想是4个数中取两个,
计算后变为3个数,再取2个计算,直到结果为1个数为止,若其值为24就输出该组表达式。
依次遍历所有表达式组合,可以找出所有满足条件的解。
题中规定方案不能重复,加法乘法交换律算不同方案。但如果四个数中有重复的,那么结果就会出现完全重复的情况。
例如:输入2 4 4 5 就会出现2个(5-2)x(4+4)的表达式
虽然这也是交换律导致的,但貌似不符合要求,不知道该怎么解决

这绝对是个C程序,不知道为什么CSDN歧视C.......只好选C++了

]#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

#define Threshold  1e-6	//浮点数精度
#define CardsNumber 4	//纸牌数目
#define ResultValue  24	//最终结果

typedef char string[20];
string result[CardsNumber];	//用于存储表达式
double number[CardsNumber];	//用于存储初始数字及中间运算值
int success=0;	//标志位,1表示有解

/************************************************
函数名:PointGame
参数:类型int 表示纸牌数目
返回值:类型int 表示是否有解,有解返回1,否则返回0
作用:输出任意个数字通过+、-、X、/、()
运算符组成的表达式的值等于指定结果的表达式
*************************************************/
int PointGame(int n)
{
int i,j,len;
if(n == 1)
{
if(fabs(number[0] - ResultValue) < Threshold)	//比较最终结果是否等于24,结果为浮点数
{
//输出最终表达式,去掉外层括号
len=strlen(result[0]);
result[0][len-1]='/0';
printf("%s/n",&result[0][1]);
success=1;
return 1;
}
else
{
return 0;
}

}

for(i = 0; i < n; i++)
{
for(j = i +1 ; j < n; j++)
{
double a, b;
string expa, expb;

a = number[i];
b = number[j];
number[j] = number[n - 1];

strcpy(expa,result[i]);
strcpy(expb,result[j]);
strcpy(result[j],result[n-1]);

//加法操作 a+b
number[i] = a + b;
strcpy(result[i],"(");
strcat(result[i],expa);
strcat(result[i],"+");
strcat(result[i],expb);
strcat(result[i],")");

PointGame(n-1);

//减法操作 a-b
number[i] = a - b;
strcpy(result[i],"(");
strcat(result[i],expa);
strcat(result[i],"-");
strcat(result[i],expb);
strcat(result[i],")");

PointGame(n-1);

//减法操作 b-a
number[i] = b - a;
strcpy(result[i],"(");
strcat(result[i],expb);
strcat(result[i],"-");
strcat(result[i],expa);
strcat(result[i],")");

PointGame(n-1);

//乘法操作 a*b
number[i] = a * b;
strcpy(result[i],"(");
strcat(result[i],expa);
strcat(result[i],"x");
strcat(result[i],expb);
strcat(result[i],")");

PointGame(n-1);

//除法操作 a/b, 如果除数不为0
if(b != 0)
{
number[i] = a / b;
strcpy(result[i],"(");
strcat(result[i],expa);
strcat(result[i],"/");
strcat(result[i],expb);
strcat(result[i],")");

PointGame(n-1);

}

//除法操作 b/a , 如果除数不为0
if(a != 0)
{
number[i] = b / a;
strcpy(result[i],"(");
strcat(result[i],expb);
strcat(result[i],"/");
strcat(result[i],expa);
strcat(result[i],")");

PointGame(n-1);

}

//将a,b放回数组
number[i] = a;
number[j] = b;
strcpy(result[i],expa);
strcpy(result[j],expb);

}
}
return success;

}

void main()
{
int x,i;
for(i =0; i< CardsNumber; i++)
{
printf("the %dth number:",i);
scanf("%d",&x);
number[i] = x;
itoa(x,result[i],10);
}

printf("/n");
if(0==PointGame(CardsNumber))
{
printf("无解!/n");

}

system("pause");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: