递归的函数 SDUT 2176

本文介绍了一个复杂的递归函数实现,该函数根据不同条件返回特定值。文章详细解释了递归调用的过程,并通过示例展示了如何根据输入计算结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

递归的函数

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

#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <map>
#include <cmath>
#include <ctime>
#include <stack>
#include <set>
#include <bits/stdc++.h>
using namespace std;
int s[31][31][31]={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(s[a][b][c])
        return s[a][b][c];
    else 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);
    }
}
int main()
{
    int a,b,c,ans;
    memset(s,0,sizeof(s));
    while(cin>>a>>b>>c)
    {
        ans=f(a,b,c);
        cout<<ans<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值