标题:求大神,写下递归调用的步骤,即红色部分,尽量详细点
取消只看楼主
平常心q
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:120
专家分:550
注 册:2016-3-31
结帖率:75%
已结贴  问题点数:20 回复次数:4 
求大神,写下递归调用的步骤,即红色部分,尽量详细点
# include <stdio.h>

int fun(int m, int n)        //m个苹果 n个篮子
{
    int i, t = 0;
   
    if(n <= 1)
        return 1;
   
    for(i=m; i>=0; i-=n)
        t += fun(i, n-1);
   
    return t;
}

int main(void)
{
    int m, n;
   
    printf("请输入m个苹果 n个篮子:");
    scanf("%d %d", &m, &n);
   
    printf("放法:%d种!\n", fun(m, n));
   
    return 0;
}
搜索更多相关主题的帖子: include return 苹果 
2016-07-11 15:24
平常心q
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:120
专家分:550
注 册:2016-3-31
得分:0 
fun(7, 3);时:
                i = 7时, t = fun(7, 2);
                i = 4时, t = fun(7, 2) + fun(4, 2);
                i = 1时, t = fun(7, 2) + fun(4, 2) + fun(1, 2);

                fun(7, 2);时:
                            i = 7时, t = fun(7, 1);
                            i = 5时, t = fun(7, 1) + fun(5, 1);
                            i = 3时, t = fun(7, 1) + fun(5, 1) + fun(3, 1);
                            i = 1时, t = fun(7, 1) + fun(5, 1) + fun(3, 1) + fun(1, 1);
                此时: t = 4; 即: fun(7, 2) = 4;

                fun(4, 2);时:
                            i = 4时, t = fun(4, 1);
                            i = 2时, t = fun(4, 1) + fun(2, 1);
                            i = 0时, t = fun(4, 1) + fun(2, 1) + fun(0, 1);
                此时: t = 3; 即: fun(4, 2) = 3;

                fun(1, 2);时:
                            i = 1时, t = fun(1, 1);
                此时: t = 1; 即: fun(1, 2) = 1;
   
    综上, t = 8; 即: fun(7, 3) = 8;

求正解
2016-07-11 19:36
平常心q
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:120
专家分:550
注 册:2016-3-31
得分:0 
回复 4楼 linlulu001
那我想问下,以上我写的调用过程正确吗?!
2016-07-11 22:55
平常心q
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:120
专家分:550
注 册:2016-3-31
得分:0 
回复 8楼 linlulu001
能指出来吗,好让我清楚哪有问题啊
2016-07-12 17:53
平常心q
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:7
帖 子:120
专家分:550
注 册:2016-3-31
得分:0 
回复 10楼 linlulu001
谢谢你的回答!
2016-07-12 22:56



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-466836-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.097790 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved