当前位置: 代码迷 >> 综合 >> 递归的函数 oj
  详细解决方案

递归的函数 oj

热度:62   发布时间:2024-01-14 22:43:06.0

递归的函数

Time Limit: 1000 ms  Memory Limit: 65536 KiB
Submit  Statistic
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
#include <stdio.h>
#include <stdlib.h>
int d[21][21][21]={0};
int f(int a,int b,int c)
{if(a<=0||b<=0||c<=0)return 1;if(a>20||b>20||c>20)return f(20,20,20);if(d[a][b][c]){return d[a][b][c];}else if(a<b&&b<c)return d[a][b][c]=f(a,b,c-1)+f(a,b-1,c-1)-f(a,b-1,c);elsereturn d[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);
}
int main()
{int a,b,c;int k;while(~scanf("%d%d%d",&a,&b,&c)){k=f(a,b,c);printf("%d\n",k);}return 0;
}
  相关解决方案