您的位置:首页 > 大数据 > 人工智能

OpenJ_Bailian - 2787 算24 【思维 + dfs】

2017-06-22 17:44 459 查看
传送门

思路 : 因为要问是否可以算出, 所以需要把所有可能性都列出来, 自然就要想到搜素赛.

AC Code

#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstring>
#define db double
#define CLR(x) memset(x,0,sizeof(x))
using namespace std;
const db eps=1e-5;
db a[5];
int vis[5];
int dfs(int step)
{
if(step==1){
if(fabs(a[0]-24)<eps)
return 1;
else return 0;
}
for(int i=0;i<step-1;i++){
for(int j=i+1;j<step;j++){
db x=a[i];
db y=a[j];
a[j]=a[step-1];
a[i]=x+y;

if(dfs(step-1))
return 1;

a[i]=x-y;
if(dfs(step-1))
return 1;

a[i]=y-x;
if(dfs(step-1))
return 1;

a[i]=y*x;
if(dfs(step-1))
return 1;

a[i]=x/y;
if(dfs(step-1))
return 1;

a[i]=y/x;
if(dfs(step-1))
return 1;

a[i]=x;
a[j]=y;
}
}
return 0;
}
int main()
{
while(~scanf("%lf%lf%lf%lf",&a[0],&a[1],&a[2],&a[3])){
CLR(vis);
if(a[1]+a[2]+a[3]+a[0]==0){
break;
}
if(dfs(4)){
printf("YES\n");
}
else
printf("NO\n");
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: