救命,要死人了

P1464 Function

LeonTexius @ 2024-11-15 15:12:12

#include<bits/stdc++.h>
using namespace std;
long long record[22][22][22];

long long w(long long a,long long b,long long c)
{

    if(a <= 0 || b <= 0 || c <= 0)
    {
        return 1;

    }
    if(record[a][b][c])
    {
        return record[a][b][c];

    }
    if(a > 20 || b > 20 || c > 20)
    {
        record[20][20][20] = w(20,20,20);

        return record[20][20][20];

    }

    if(a < b && b < c)
    {

        record[a][b][c] = w(a,b,c - 1) + w(a,b - 1,c - 1) - w(a,b - 1,c);

    }
    else
    {
        record[a][b][c] = w(a,b - 1,c) + w(a - 1,b - 1,c) + w(a - 1,b,c - 1) - w(a - 1,b - 1,c - 1);

    }
    return record[a][b][c];

}

int main()
{

    long long aa = 0,bb = 0,cc = 0;
    while(!(aa == -1 && bb == -1 && cc == -1))
    {
        cin >> aa >> bb >> cc;
        printf("w(%lld, %lld, %lld) = %lld\n",aa,bb,cc,w(aa,bb,cc));

    }

    return 0;

}

by lizexi707 @ 2024-11-28 16:58:55

那个递归函数下面的return record可有可无 以下是代码:


#include<bits/stdc++.h> 
using namespace std;
long long a,b,c;
long long f[25][25][25];
long long w(long long a,long long b,long long c)
{
    if(a<=0||b<=0||c<=0)
    {
        return 1;
    }
    if(a>20 || b>20 || c>20)
    {
        return w(20,20,20);
    }
    if(f[a][b][c]!=0)
        return f[a][b][c];

    if( a<b &&b<c)
    {
        return f[a][b][c] =w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);

    }
    return f[a][b][c] =w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
}
int main()
{
    while(1){
        cin>>a>>b>>c;
        if(a==-1&&b==-1&&c==-1) 
            return 0;
        cout<<"w("<<a<<", "<<b<<", "<<c<<")"<<" = "<<w(a,b,c)<<endl;

    }

    return 0;
}

|