CSU1600:Twenty-four point(DFS)

Description

Given four numbers, can you get twenty-four through the addition, subtraction, multiplication, and division? Each number can be used only once.

Input

The input consists of multiple test cases. Each test case contains 4 integers A, B, C, D in a single line (1 <= A, B, C, D <= 13).

Output

For each case, print the “Yes” or “No”. If twenty-four point can be get, print “Yes”, otherwise, print “No”.

Sample Input

2 2 3 9
1 1 1 1 
5 5 5 1

Sample Output

Yes
No
Yes

HINT

For the first sample, (2/3+2)*9=24.

Source


题意:四个数字,通过四则运算看能不能得到24点
思路:对于如此小量的数,直接暴搜,你不需要怀疑人生

#include <iostream> 
#include <stdio.h> 
#include <string.h> 
#include <stack> 
#include <queue> 
#include <map> 
#include <set> 
#include <vector> 
#include <math.h> 
#include <algorithm> 
using namespace std; 
#define ls 2*i 
#define rs 2*i+1 
#define up(i,x,y) for(i=x;i<=y;i++) 
#define down(i,x,y) for(i=x;i>=y;i--) 
#define mem(a,x) memset(a,x,sizeof(a)) 
#define w(a) while(a) 
#define LL long long 
const double pi = acos(-1.0); 
#define Len 200005 
#define mod 19999997 
const int INF = 0x3f3f3f3f; 
#define exp 1e-8 
  
double a[5]; 
  
bool dfs(double *a,int n) 
{ 
    if(n==1) return fabs(a[0]-24)<exp; 
    int i,j,k,l; 
    double tem[5]; 
    up(i,0,n-1) 
    { 
        up(j,i,n-1) 
        { 
            if(i==j) continue; 
            int len = 0; 
            up(l,0,n-1) 
            { 
                if(l!=i && l!=j) 
                    tem[len++]=a[l]; 
            } 
            tem[len] = a[i]+a[j]; 
            if(dfs(tem,n-1)) return true; 
            tem[len] = a[i]-a[j]; 
            if(dfs(tem,n-1)) return true; 
            tem[len] = a[j]-a[i]; 
            if(dfs(tem,n-1)) return true; 
            tem[len] = a[i]*a[j]; 
            if(dfs(tem,n-1)) return true; 
            if(a[i]!=0) 
            { 
                tem[len] = a[j]/a[i]; 
                if(dfs(tem,n-1)) return true; 
            } 
            if(a[j]!=0) 
            { 
                tem[len] = a[i]/a[j]; 
                if(dfs(tem,n-1)) return true; 
            } 
        } 
    } 
    return false; 
} 
  
int main() 
{ 
    w(~scanf("%lf%lf%lf%lf",&a[0],&a[1],&a[2],&a[3])) 
    { 
        if(dfs(a,4)) 
            printf("Yes\n"); 
        else
            printf("No\n"); 
    } 
    return 0; 
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值