求助!!!

P1464 Function

bj12z_JiaTangchen @ 2023-04-29 08:52:56

#include <bits/stdc++.h>
using namespace std;
int f[21][21][21];
int 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);
    int k;
    if(f[a][b][c]) return f[a][b][c];
    if(a<b&&b<c) k=w(a,b,c-1)+w(a,b-1,c-1)+w(a,b-1,c);
    else k=w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
    f[a][b][c]=k;
    return k;
}
int main()
{
    long long a,b,c;
    cin>>a>>b>>c;
    while(true){
        if(a==-1&&b==-1&&c==-1) break;
        cout<<"w("<<a<<","<<b<<","<<c<<")=";
        cout<<w(a,b,c)<<endl;
        cin>>a>>b>>c;
    }
    return 0;
}

by IRIDESCENTqwq @ 2023-04-29 09:26:07

1、第9行,w(a,b-1,c-1)w(a,b-1,c)之间是-号连接;

2、第20行,输出格式不对,注意要加空格。


by Pekopeko @ 2023-04-29 19:52:58

while循环的判断换成cin>>a>>b>>c会不会更好一点(雾)


by bj12z_JiaTangchen @ 2023-04-30 09:24:47

@YiLino 谢谢大佬,成功AC了


by bj12z_JiaTangchen @ 2023-04-30 09:26:00

@bj12z_liuzhengyue 好像可以,放里面之后AC了,谢谢大佬!


by bj12z_JiaTangchen @ 2023-04-30 09:26:44

#include <bits/stdc++.h>
using namespace std;
int f[21][21][21];
int 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);
    int k;
    if(f[a][b][c]) return f[a][b][c];
    if(a<b&&b<c) k=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
    else k=w(a-1,b,c)+w(a-1,b-1,c)+w(a-1,b,c-1)-w(a-1,b-1,c-1);
    f[a][b][c]=k;
    return k;
}
int main()
{
    long long a,b,c;
    while(cin>>a>>b>>c){
        if(a==-1&&b==-1&&c==-1) break;
        cout<< "w(" << a <<", " << b << ", " << c << ") = " ;
        cout<<w(a,b,c)<<endl;

    }
    return 0;
}

|