📌  相关文章
📜  计数到达第 n 个楼梯的方法的Java程序

📅  最后修改于: 2022-05-13 01:58:09.519000             🧑  作者: Mango

计数到达第 n 个楼梯的方法的Java程序

有n个楼梯,站在底部的人想爬到顶部。此人一次可以爬 1 个楼梯或 2 个楼梯。数一数路数,人能登顶。

楼梯

考虑图中所示的示例。 n 的值为 3。有 3 种方法可以到达顶部。该图取自 Easier Fibonacci 谜题

Java
class stairs {
    // A simple recursive program to find n'th fibonacci number
    static int fib(int n)
    {
        if (n <= 1)
            return n;
        return fib(n - 1) + fib(n - 2);
    }
  
    // Returns number of ways to reach s'th stair
    static int countWays(int s)
    {
        return fib(s + 1);
    }
  
    /* Driver program to test above function */
    public static void main(String args[])
    {
        int s = 4;
        System.out.println("Number of ways = " + countWays(s));
    }
} /* This code is contributed by Rajat Mishra */


Java
class stairs {
    // A recursive function used by countWays
    static int countWaysUtil(int n, int m)
    {
        if (n <= 1)
            return n;
        int res = 0;
        for (int i = 1; i <= m && i <= n; i++)
            res += countWaysUtil(n - i, m);
        return res;
    }
  
    // Returns number of ways to reach s'th stair
    static int countWays(int s, int m)
    {
        return countWaysUtil(s + 1, m);
    }
  
    /* Driver program to test above function */
    public static void main(String args[])
    {
        int s = 4, m = 2;
        System.out.println("Number of ways = " + countWays(s, m));
    }
} /* This code is contributed by Rajat Mishra */


Java
// Java program to count number of ways to reach n't stair when
// a person can climb 1, 2, ..m stairs at a time
  
class GFG {
    // A recursive function used by countWays
    static int countWaysUtil(int n, int m)
    {
        int res[] = new int[n];
        res[0] = 1;
        res[1] = 1;
        for (int i = 2; i < n; i++) {
            res[i] = 0;
            for (int j = 1; j <= m && j <= i; j++)
                res[i] += res[i - j];
        }
        return res[n - 1];
    }
  
    // Returns number of ways to reach s'th stair
    static int countWays(int s, int m)
    {
        return countWaysUtil(s + 1, m);
    }
  
    // Driver method
    public static void main(String[] args)
    {
        int s = 4, m = 2;
        System.out.println("Number of ways = " + countWays(s, m));
    }
}


输出:
Number of ways = 5

上述实现的时间复杂度是指数级的(黄金比例提高到 n 次方)。可以使用前面讨论的斐波那契函数优化来优化它以在 O(Logn) 时间内工作。

Java

class stairs {
    // A recursive function used by countWays
    static int countWaysUtil(int n, int m)
    {
        if (n <= 1)
            return n;
        int res = 0;
        for (int i = 1; i <= m && i <= n; i++)
            res += countWaysUtil(n - i, m);
        return res;
    }
  
    // Returns number of ways to reach s'th stair
    static int countWays(int s, int m)
    {
        return countWaysUtil(s + 1, m);
    }
  
    /* Driver program to test above function */
    public static void main(String args[])
    {
        int s = 4, m = 2;
        System.out.println("Number of ways = " + countWays(s, m));
    }
} /* This code is contributed by Rajat Mishra */
输出:
Number of ways = 5

Java

// Java program to count number of ways to reach n't stair when
// a person can climb 1, 2, ..m stairs at a time
  
class GFG {
    // A recursive function used by countWays
    static int countWaysUtil(int n, int m)
    {
        int res[] = new int[n];
        res[0] = 1;
        res[1] = 1;
        for (int i = 2; i < n; i++) {
            res[i] = 0;
            for (int j = 1; j <= m && j <= i; j++)
                res[i] += res[i - j];
        }
        return res[n - 1];
    }
  
    // Returns number of ways to reach s'th stair
    static int countWays(int s, int m)
    {
        return countWaysUtil(s + 1, m);
    }
  
    // Driver method
    public static void main(String[] args)
    {
        int s = 4, m = 2;
        System.out.println("Number of ways = " + countWays(s, m));
    }
}
输出:
Number of ways = 5

有关详细信息,请参阅有关到达第 n 级楼梯的计数方式的完整文章!