版权声明:本文为博主原创文章,未经博主允许不得转载。https://blog.csdn.net/uzzzhf https://blog.csdn.net/uzzzhf/article/details/89354852
给出4个小于10个正整数,你可以使用加减乘除4种运算以及括号把这4个数连接起来得到一个表达式。
现在的问题是,是否存在一种方式使得得到的表达式的结果等于24。
这里加减乘除以及括号的运算结果和运算的优先级跟我们平常的定义一致(这里的除法定义是实数除法)。
比如,对于5,5,5,1,我们知道5 * (5 – 1 / 5) = 24,因此可以得到24。又比如,对于1,1,4,2,我们怎么都不能得到24。
Input
输入数据包括多行,每行给出一组测试数据,包括4个小于10个正整数。
最后一组测试数据中包括4个0,表示输入的结束,这组数据不用处理。
Output
对于每一组测试数据,输出一行,如果可以得到24,输出“YES”;否则,输出“NO”。
Sample Input
5 5 5 1
1 1 4 2
0 0 0 0
Sample Output
YES
NO
#include <iostream>
#include <cmath>
using namespace std;
double a[5];//用来装数据
#define EPS 1e-6 //假设的无穷小
bool isZero(double x)
{
return fabs(x) <= EPS;
}
bool count24(double a[],int n)
{//用数组a里的 n个数,计算24:这个状态量就是核心
if( n == 1 ) //:出口
{
if(isZero( a[0] - 24) )//满足
return true;
else //不满足
return false;
}
double b[5]; //源的替代
for(int i = 0;i < n-1; ++i)//枚举两个数的组合
for(int j = i+1;j < n; ++j)
{
int m = 0; //还剩下m个数, m = n - 2
for(int k = 0; k < n; ++k)
if( k != i && k!= j)
b[m++] = a[k];//把其余数放入b
b[m] = a[i]+a[j];//两数相加的情况
if(count24(b,m+1)) //递归
return true;
b[m] = a[i]-a[j];//两数相减得情况
if(count24(b,m+1))
return true;
b[m] = a[j]-a[i]; //两数相减得情况
if(count24(b,m+1))
return true;
b[m] = a[i]*a[j];//两数相乘得情况
if(count24(b,m+1))
return true;
if( !isZero(a[j])) //分母不为 0
{
b[m] = a[i]/a[j]; //两数相除得情况
if(count24(b,m+1))
return true;
}
if( !isZero(a[i]))
{
b[m] = a[j]/a[i];
if(count24(b,m+1))
return true;
}
}
return false;//默认返回 false
}
int main()
{
while(true)
{
for(int i = 0;i < 4; ++i)//输入数据
cin >> a[i];
if( isZero(a[0]))//循环退出的条件
break;
if( count24(a,4))//进行判断
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}