http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1600
24点游戏。暴力搜索就可以了!
#include<bits/stdc++.h>
#define eps 1e-5
using namespace std;
double e[5];
int dfs(double a[],int len)
{
if(len==1) return fabs(a[0]-24)<eps;
int count=0;double f[5];
for(int i=0;i<len;i++)
{
for(int j=i+1;j<len;j++)
{
count=0;
for(int l=0;l<len;l++)
{
if(l!=i&&l!=j) f[count++]=a[l];
}
f[count]=a[i]+a[j];
if(dfs(f,count+1)) return 1;
f[count]=fabs(a[i]-a[j]);
if(dfs(f,count+1)) return 1;
f[count]=a[i]*a[j];
if(dfs(f,count+1)) return 1;
if(a[i]!=0)
{
f[count]=a[j]/a[i];
if(dfs(f,count+1)) return 1;
}
if(a[j]!=0)
{
f[count]=a[i]/a[j];
if(dfs(f,count+1)) return 1;
}
}
}
return 0;
}
int main()
{
double a,b,c,d;
while(scanf("%lf%lf%lf%lf",&a,&b,&c,&d)!=EOF)
{
e[0]=a;e[1]=b;e[2]=c;e[3]=d;
if(dfs(e,4))
{
printf("Yes\n");
}
else printf("No\n");
}
return 0;
}