1 条题解

  • 0
    @ 2026-9-30 12:58:11

    C++ :

    
    #include <cstdio>
    #include <cstdlib>
    static int ok;
    long long ab(long long x){ return x < 0 ? -x : x; }
    void dfs(int len, long long *cur){
        if(ok) return;
        if(len == 1){ if(cur[0] == 24) ok = 1; return; }
        for(int i = 0; i < len; i++)
            for(int j = 0; j < len; j++){
                if(i == j) continue;
                long long rest[5]; int rc = 0;
                for(int k = 0; k < len; k++) if(k != i && k != j) rest[rc++] = cur[k];
                long long cand[8]; int cc = 0;
                cand[cc++] = cur[i] + cur[j];
                cand[cc++] = cur[i] - cur[j];
                cand[cc++] = cur[i] * cur[j];
                /* 全程不得出现小数:只在整除时才允许除法 */
                if(cur[j] != 0 && cur[i] % cur[j] == 0) cand[cc++] = cur[i] / cur[j];
                for(int t = 0; t < cc; t++){
                    long long nx[5];
                    nx[0] = cand[t];
                    for(int k = 0; k < rc; k++) nx[k + 1] = rest[k];
                    dfs(len - 1, nx);
                    if(ok) return;
                }
            }
    }
    int main(){
        static char tk[16];
        while(scanf("%15s", tk) == 1){
            long long q[4];
            for(int i = 0; i < 4; i++){
                if(i) if(scanf("%15s", tk) != 1) return 0;
                if(tk[0] == 'A') q[i] = 1;
                else if(tk[0] == 'J') q[i] = 11;
                else if(tk[0] == 'Q') q[i] = 12;
                else if(tk[0] == 'K') q[i] = 13;
                else q[i] = atoll(tk);
            }
            ok = 0;
            dfs(4, q);
            printf("%s\n", ok ? "Yes" : "No");
        }
        return 0;
    }
    
    
    • 1

    信息

    ID
    2834
    时间
    1000ms
    内存
    256MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者