版权声明:欢迎转载,也请注明原文地址 https://blog.csdn.net/wzy_2017/article/details/80015568
递归的函数
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Problem Description
给定一个函数 f(a, b, c):
如果 a ≤ 0 或 b ≤ 0 或 c ≤ 0 返回值为 1;
如果 a > 20 或 b > 20 或 c > 20 返回值为 f(20, 20, 20);
如果 a < b 并且 b < c 返回 f(a, b, c−1) + f(a, b−1, c−1) − f(a, b−1, 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)。
看起来简单的一个函数?你能做对吗?
Input
输入包含多组测试数据,对于每组测试数据:
输入只有一行为 3 个整数a, b, c(a, b, c < 30)。
Output
对于每组测试数据,输出函数的计算结果。
Sample Input
1 1 1 2 2 2
Sample Output
2 4
Hint
Source
qinchuan
import java.util.*;
public class Main {
static int s[][][]=new int[33][33][33];
static int f(int a,int b,int c)
{
if(a<=0||b<=0||c<=0)
return 1;//因为题目中描述的判定条件有交叉的部分,所以要提前return
if(a>20||b>20||c>20)
return s[a][b][c]=f(20,20,20);
if(s[a][b][c]!=0)
return s[a][b][c];
if(a<b&&b<c)
return s[a][b][c]=f(a,b,c-1)+f(a,b-1,c-1)-f(a,b-1,c);
else
return 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);
}
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
while(cin.hasNext())
{
int a=cin.nextInt();
int b=cin.nextInt();
int c=cin.nextInt();
int ans = f(a,b,c);
System.out.println(ans);
}
cin.close();
}
}