24点题目

题目入口

#include<bits/stdc++.h>
#include<unordered_map>
#include<math.h>
#include<forward_list>
using namespace std;
#define re register
#define ll long long
#define endl '\n'
#define pii pair<ll,ll>
#define IOS std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fi first
#define se second
#define rad read
#define lson (o<<1)
#define rson (o<<1|1)
#define mid (l+r>>1)
ll mod = 100003;
const int N = 5e6 + 10;
const int M = 200002;
const long long INF = LLONG_MAX;
inline ll  gcd(ll a, ll b) {
    return b ? gcd(b, a % b) : a;
}
inline ll exgcd(ll a, ll b, ll& x, ll& y) {
    //ax+by=1
    if (!b) {
        x = 1, y = 0;
        return a;
    }
    exgcd(b, a % b, y, x);
    y -= (a / b) * x;
    return b;
}
inline ll ksc(ll a, ll b) {

    ll ans = 0;
    while (b) {
        if (b & 1) {
            ans = (ans + a) % mod;
        }
        a = (a + a) % mod;
        b >>= 1;
    }
    return ans;
}
ll qpow(ll a, ll b) {
    ll ans = 1;
    while (b) {
        if (b & 1)
            ans = (ans % mod * a % mod) % mod;
        a = (a % mod * a % mod) % mod;
        b >>= 1;
    }
    return ans;

}
inline ll read() {
    ll s = 0; bool w = true;
    char c = getchar();
    for (; c < '0' || c>'9'; c = getchar())if (c == '-')w = false;
    for (; c >= '0' && c <= '9'; c = getchar())s = (s << 1) + (s << 3) + (c ^ 48);
    return w ? s : -s;
}
inline void fp(ll n) {
    if (n < 0)  putchar('-'), n = -n;
    if (n > 9) fp(n / 10);
    putchar(n % 10 + 48);
}
int a[5], b, c, d;
char s[5] = { ' ','+','-','*','/' };
double ss(double x, double y, char c) {
    if (c == '*')return x * y * 1.0;
    if (c == '+')return x + y * 1.0;
    if (c == '-')return x - y * 1.0;
    if (c == '/') {
        if (y == 0)return 0;
        else return x * 1.0 / y;
    }
    return 0;
}
bool ping1(int a, int b, int c, int d) {
    //(a,b),(c,d)
    for(int i=1;i<=4;++i)
        for (int j = 1; j <= 4; ++j) {
            //if (i == j)continue;
            for (int k = 1; k <= 4; ++k) {
                //if (i == k || j == k)continue;
                if (fabs(ss(ss(a, b, s[i]), ss(c, d, s[k]), s[j])-24)<0.000001) {
                    printf("(%d%c%d)%c(%d%c%d)",a,s[i],b,s[j],c,s[k],d);
                        exit(0);
                }
                
            }
        }
    return false;
}
bool ping2(int a, int b, int c, int d) {
    //((a,b),c),d
    for (int i = 1; i <= 4; ++i)
        for (int j = 1; j <= 4; ++j) {
            //if (i == j)continue;
            for (int k = 1; k <= 4; ++k) {
                //if (i == k || j == k)continue;
                if (fabs(ss(ss(ss(a, b, s[i]),c,s[j]),d,s[k]) - 24 )< 0.000001) {
                    printf("((%d%c%d)%c%d)%c%d", a,s[i],b,s[j],c,s[k],d);
                    exit(0);
                }

            }
        }
    return false;
}
bool ping3(int a, int b, int c, int d) {
    //a,((b,c),d)
    for (int i = 1; i <= 4; ++i)
        for (int j = 1; j <= 4; ++j) {
            //if (i == j)continue;
            for (int k = 1; k <= 4; ++k) {
                //if (i == k || j == k)continue;
                if (fabs(ss(a,ss(ss(b,c,s[j]),d,s[k]), s[i]) - 24) < 0.000001) {
                    printf("%d%c((%d%c%d)%c%d)", a,s[i],b,s[j],c,s[k],d);
                    //fabs(ss(a, ss(ss(b, c, s[j]), d, s[k]), s[i]) - 24);
                    exit(0);
                }

            }
        }
    return false;
}
bool ping4(int a, int b, int c, int d) {
    //(a,(b,c)),d
    for (int i = 1; i <= 4; ++i)
        for (int j = 1; j <= 4; ++j) {
            //if (i == j)continue;
            for (int k = 1; k <= 4; ++k) {
                //if (i == k || j == k)continue;
                if (fabs(ss(ss(a,ss(b,c,s[j]),s[i]),d,s[k]) - 24) < 0.000001) {
                    printf("(%d%c(%d%c%d))%c%d)", a,s[i],b,s[j],c,s[k],d);
                    fabs(ss(ss(ss(b, c, s[j]), a, s[i]), d, s[k]) - 24);
                    exit(0);
                }

            }
        }
    return false;
}
bool ping5(int a, int b, int c, int d) {
    //a,(b,(c,d))
    for (int i = 1; i <= 4; ++i)
        for (int j = 1; j <= 4; ++j) {
            //if (i == j)continue;
            for (int k = 1; k <= 4; ++k) {
                //if (i == k || j == k)continue;
                if (fabs(ss(a,ss(b,ss(c,d,s[k]), s[j]), s[i]) - 24) < 0.000001) {
                    printf("%d%c(%d%c(%d%c%d))", a, s[i], b, s[j], c, s[k], d);
                    exit(0);
                }

            }
        }
    return false;
}
bool ping(int x, int y, int z, int w) {
    if (ping2(x, y, z, w))return true;
    if (ping1(x, y, z, w))return true;
    if (ping3(x, y, z, w))return true;
    if (ping5(x, y, z, w))return true;
    if (ping4(x, y, z, w))return true;
    return false;
}
signed main(){
    for (int i = 0; i < 4; ++i)cin >> a[i];
    /*string k;
    for (int i = 0; i < 4; ++i) {
        cin >> k;
        if (k.size() == 1) {
            if (k[0] == 'J')a[i] = 11;
            else if (k[0] == 'Q')a[i] = 12;
            else if (k[0] == 'K')a[i] = 13;
            else a[i] = k[0] - '0';
        }
        else a[i] = stoi(k);
    }        大意了*/
    //a = l[0], b = l[1], c = l[2], d = l[3];
    //cout << a[0] << ' ' << a[1] <<' ' << a[2] << ' ' << a[3] << endl;
    for (int i = 0; i < 4; ++i) 
        for(int j=0;j<4;++j)
            for(int k=0;k<4;++k)
                for (int l = 0; l < 4; ++l) {
                    if (i == j || i == k || i == l || j == k || j == l || k == l)continue;
                    for (int w = 0; w < 4; ++w)  if (ping(a[i], a[j], a[k], a[l])) { return 0; }
                }
    cout << -1;
return  0;

}
/**
*  ┏┓  WQJ ┏┓+ +
* ┏┛┻━━━┛┻┓ +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃       ┣┓>>>
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
2022/11/22/18:22---2022/11/22/19:33
*/

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值