40分求助,#1234AC,#567WA

P1464 Function

zzh6666663 @ 2023-09-27 02:15:29

#include<iostream>
#include<cstring>
#define index -1
using namespace std;

long long w(long long a, long long b, long long c);
long long mem[25][25][25];//哈希记忆数组,初始化为-1

int main(){
    memset(mem, index, sizeof(mem));
    long long a, b, c;

    while(cin >> a >> b >> c){
        if(a==-1 && b==-1 && c==-1){
            break;
        }
        cout << "w("  << a << ", " << b << ", " << c << ") = ";
        if(a>20){
            a = 21;
        }else if(b>20){
            b = 21;
        }else if(c>20){
            c = 21;
        }

        cout << w(a, b, c) << endl;
    }

    return 0;
}

long long w(long long a, long long b, long long c){
    if(a<=0 || b<=0 || c<=0){
        return 1;
    }

    long long &hash = mem[a][b][c];

    if(hash != -1){
        return hash;
    }

    if(a>20 || b>20 || c>20){
        hash = w(20, 20, 20);
    }else if(a<b && b<c){
        hash = w(a, b, c-1) + w(a, b-1, c-1) - w(a, b-1, c);
    }else{
        hash = w(a-1, b, c) + w(a-1, b-1, c) + w(a-1, b, c-1) - w(a-1, b-1, c-1);
    }

    return hash;
}

by zzh6666663 @ 2023-10-18 20:37:33

好吧,我整明白了 当a,b,c超过20的时候要用三个单独的if来赋值,不能用if else,不然当三个数都超过20时,只赋值了第一个数就会跳出语句,从而wa


|