为什么我在返回F(n-1,t)+ F(n,t-1)线上有错误?

时间:2015-04-14 03:41:38

标签: java arrays recursion

问题:

从数学上证明,以下2D递归关系控制值:

                      F(n,t) = F(n-1,t) + F(n,t-1)

现在,回答以下问题:

(b)通过使用递归方法,实现上述关系。

(c)使用(5,5),(10,10)和(14,14)测试你的方法

//主要方法

public static void main(String[] args) {
    // TODO code application logic here

    TwoDimension a = new TwoDimension();
    a.F(5, 5);
    a.F(10, 10);
    a.F(14, 14);
}

//类

public static class TwoDimension {

    public TwoDimension() {

    }

    public long F(int n, int t) {

            if (t == 1) {
                return 1;
            } else {
                return F(n - 1, t) + F(n, t - 1);
            }


    }
}

这是输出

Exception in thread "main" java.lang.StackOverflowError
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
at project2.Project2$TwoDimension.F(Project2.java:37)
请帮助meeeee,如何修复返回方法?

2 个答案:

答案 0 :(得分:0)

在您的代码中

F(n, t - 1);

将评估t的基本条件。但是

 F(n - 1, t)

你传递的值与t相同。因此它永远不会改变,永远不会达到t的基本条件。 除此之外,你也没有任何基本价值。

答案 1 :(得分:0)

在你的功能中:

public long F(int n,int t){

        if (t == 1) {
            return 1;
        } else {
            return F(n - 1, t) + F(n, t - 1);
        }


}

对于这种情况,F(n - 1, t)t将始终保持不变,因此永远不会达到t==1,并且递归不会结束。

您需要为n设置一个条件,而在此情况下,您可能还需要检查负数:)