Squirrel_GZQ @ 2024-05-05 12:37:29
运行到15 15 15都是没有问题的
#include<bits/stdc++.h>
using namespace std;
long long g[25][25][25];
bool vis[25][25][25];
int w(long long a, long long b, long long c){
if(a>25||b>25||c>25) g[a][b][c]= w(20,20,20);
if(vis[a][b][c]==1) return g[a][b][c];
else if(a<=0||b<=0||c<=0){
g[a][b][c]= 1;
}
else if(a>20||b>20||c>20){
g[a][b][c]= w(20,20,20);
}
else if(a<b&&b<c){
g[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
}
else{
g[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);
}
vis[a][b][c]=1;
return g[a][b][c];
}
int main(){
long long a,b,c;
while(cin>>a>>b>>c){
if(a==-1&&b==-1&&c==-1) return 0;
printf("w(%d, %d, %d) = %lld\n",a,b,c,w(a,b,c));
}
return 0;
}
by Qiu101029 @ 2024-05-05 15:34:50
@Squirrel_GZQ
long long g[25][25][25];
if(a>25||b>25||c>25) g[a][b][c]= w(20,20,20);
这里会越界
by Qiu101029 @ 2024-05-05 15:37:12
而且还不止这点问题
by Qiu101029 @ 2024-05-05 15:49:50
#include<bits/stdc++.h>
using namespace std;
long long g[25][25][25];
bool vis[25][25][25];
//避免所有可能的越界
int w(long long a, long long b, long long c){
if(a<=0||b<=0||c<=0){
return 1;//直接返回1
}
else if(a>20||b>20||c>20){
return w(20,20,20);//同理
}
else if(vis[a][b][c]==1){
return g[a][b][c];
}
else if(a<b&&b<c){
g[a][b][c]=w(a,b,c-1)+w(a,b-1,c-1)-w(a,b-1,c);
}
else{
g[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);
}
vis[a][b][c]=1;
return g[a][b][c];
}
int main(){
long long a,b,c;
while(cin>>a>>b>>c){
if(a==-1&&b==-1&&c==-1) return 0;
printf("w(%lld, %lld, %lld) = %d\n",a,b,c,w(a,b,c));//%d对应int类型,%lld对应long long类型
}
return 0;
}
by Squirrel_GZQ @ 2024-05-05 21:19:43
@Qiu101029 谢谢谢谢! 这么久终于有人回我了!
by Squirrel_GZQ @ 2024-05-05 21:26:38
@Qiu101029 已关