HIHO #1304 : 搜索一·24点

来源:互联网 发布:单片机蜂鸣器音乐程序 编辑:程序博客网 时间:2024/05/21 06:35

题目链接

数据少,直接暴力,枚举4个数字的排列,然后再枚举3个符号的情况

因为4个数字,加括号有以下几个情况:
(a b c d )
(a b) c d, a (b c) d, a b (c d)
(a b c) d, a (b c d)
(a b) (c d)
上面的前6个只是改变了计算的顺序,我们通过数字的排列可以得到,但是最后一个情况是无法得到的,所以要另外判断

#include<bits/stdc++.h>using namespace std;#define cl(a,b) memset(a,b,sizeof(a))#define fastIO ios::sync_with_stdio(false);cin.tie(0);#define LL long long#define pb push_back#define gcd __gcd#define For(i,j,k) for(int i=(j);i<=k;i++)#define lowbit(i) (i&(-i))#define _(x) printf("%d\n",x)typedef vector<LL> vec;const double EPS = 1e-8;const int maxn = 5e6+10;const int inf  = 1 << 28;bool flag ;double two(int ty,double a,double b){    if(ty==1)return a+b;    else if(ty==2)return a-b;    else if(ty==3)return a*b;    else if(ty==4 && b!=0)return a/b;    else if(ty==5 && a!=0)return b/a;    else if(ty==6)return b-a;    else return 0;}int a[5];void work(){    For(i,1,6)For(j,1,6)For(k,1,6){        double ans = two(i,a[0],two(j,a[1],two(k,a[2],a[3])));// a op b op c op d        double ans2 = two(i,two(j,a[0],a[1]),two(k,a[2],a[3]));//(a op b) op (c op d)        if(ans == 24|| ans2 == 24){flag = true;}        if(flag){            return ;        }    }}int main(){    int T;cin>>T;    while(T--){        for(int i=0;i<4;i++)cin>>a[i];        sort(a,a+4);flag = false;        do{            work();            if(flag) break;        }while(next_permutation(a,a+4));        if(!flag)cout<<"No"<<endl;        else cout<<"Yes"<<endl;    }    return 0;}
0 0
原创粉丝点击