top1编程
← 返回题目
题解

【入门】算24点

1 条题解

  • 0
    @ 2026-7-29 0:18:25
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    typedef string ls;
    typedef long double ld;
    typedef char lr;
    
    ll a[4];
    
    bool js(int o1,int o2,int o3){
    
        ll n[4]={a[0],a[1],a[2],a[3]};
        int o[3]={o1,o2,o3};
    
        for(int i=0;i<3;i++){
    
            if(o[i]==2){
    
                n[i]=n[i]*n[i+1];
                for(int j=i+1;j<3;j++)n[j]=n[j+1];
                for(int j=i;j<2;j++)o[j]=o[j+1];
                o[2]=-1;
                i--;
    
            }
    
        }
    
        ll r=n[0];
        for(int i=0;i<3;i++){
    
            if(o[i]==0)r+=n[i+1];
            else if(o[i]==1)r-=n[i+1];
    
        }
    
        return r==24;
    }
    
    bool pd(){
    
        for(int x=0;x<3;x++)
            for(int y=0;y<3;y++)
                for(int z=0;z<3;z++)
                    if(js(x,y,z))return 1;
        return 0;
    }
    
    int main(){
    
        ll n;
        cin>>n;
        ll s=0;
    
        for( int i=0;i<n;i++ ){
    
            for( int j=0;j<4;j++ ){
    
                cin>>a[j];
    
            }
            if(pd()){
    
                s++;
    
            }
    
        }	
        cout<<s;
    
        return 0;
    }
    
    • 1