1、题意:将指定的递归函数输出其指定值;
2、思路:此函式递归方程已给出,但是函数递归过程中循环重复许多相同的步骤,直接递归会超时,因此,需要用空间换时间:用数组存储已算出的函数结果,下次需要直接调用;
3、感想:此递归函数比较简单,因为递归函数已给出,只需加一个数组储存即可;
4、代码:
#include<iostream>
using namespace std;
int s[31][31][31];
int f(int a,int b,int c)
{
if(a<=0||b<=0||c<=0)return 1;
else if(s[a][b][c]!=0)return s[a][b][c];
else if(a>20||b>20||c>20){s[a][b][c]=f(20,20,20);return s[a][b][c];}
else if(a<b&&b<c){s[a][b][c]=f(a,b,c-1)+f(a,b-1,c-1)-f(a,b-1,c);return s[a][b][c];}
else {s[a][b][c]=f(a-1,b,c)+f(a-1,b-1,c)+f(a-1,b,c-1)-f(a-1,b-1,c-1);return s[a][b][c];}
}
int main()
{
int x,y,z,i,j,k;
while(cin>>x>>y>>z)
cout<<f(x,y,z)<<endl;
}