📌  相关文章
📜  爬上第n个楼梯,允许从1到n的所有跳跃(三种不同的方法)

📅  最后修改于: 2021-04-23 21:15:44             🧑  作者: Mango

一只猴子正站在下面有N个台阶的楼梯上。考虑到一次可能需要1到N步的跳跃,请计算有多少种方法可以到达楼梯的顶部?

例子:

Input : 2
Output : 2
It can either take (1, 1) or (2) to
reach the top. So, total 2 ways

Input : 3
Output : 4
Possibilities : (1, 1, 1), (1, 2), (2, 1),
(3). So, total 4 ways

有3种不同的方式来考虑问题。

  1. 在所有可能的解决方案中,猴子要么踩到一步,要么可以跳过。因此,使用基本计数原理,第一步有2种参与方式,对于每种方式,第二步也有2种参与方式,依此类推。但是最后一步总是必须踩的。
    2 x 2 x 2 x .... x 2(N-1 th step) x 1(Nth step) 
      = 2(N-1) different ways. 
    
  2. 让我们为用例定义一个函数F(n)。 F(n)表示从底部到顶部有N个台阶的所有可能的到达方式,其中最小跃点为1步,最大跃点为N步。现在,对于猴子来说,它可以进行的第一个举动有N种不同的方式(1步,2步,3步…. N步)。如果将第一个跃点作为第一步,它将剩下N-1个要征服的步骤,可以通过F(N-1)方式实现。如果将第一个飞跃作为2步,它将覆盖N-2步,这可以用F(N-2)方式实现。放在一起,
    F(N) = F(N-1) + F(N-2) + F(N-3) + ... + 
                          F(2) + F(1) + F(0) 
    Now, 
    F(0) = 1
    F(1) = 1
    F(2) = 2
    F(3) = 4
    
    Hence,
    F(N) = 1 + 1 + 2 + 4 + ... + F(n-1)
         = 1 + 2^0 + 2^1 + 2^2 + ... + 2^(n-2)
         = 1 + [2^(n-1) - 1]
    
    C++
    // C++ program to count total number of ways
    // to reach n-th stair with all jumps alowed
    #include 
      
    int calculateLeaps(int n)
    {
        if (n == 0 || n == 1) {
            return 1;
        }
        else {
            int leaps = 0;
            for (int i = 0; i < n; i++)
                leaps += calculateLeaps(i);
            return leaps;
        }
    }
      
    // Driver code
    int main()
    {
        int calculateLeaps(int);
        std::cout << calculateLeaps(4) << std::endl;
        return 0;
    }


    Java
    // Java program to count total number of ways
    // to reach n-th stair with all jumps alowed
    class GFG {
        static int calculateLeaps(int n)
        {
            if (n == 0 || n == 1) {
                return 1;
            }
            else {
                int leaps = 0;
                for (int i = 0; i < n; i++)
                    leaps += calculateLeaps(i);
                return leaps;
            }
        }
      
        // Driver code
        public static void main(String[] args)
        {
            System.out.println(calculateLeaps(4));
        }
    }
    // This code is contributed by Anant Agarwal.


    Python3
    # Python program to count
    # total number of ways
    # to reach n-th stair with
    # all jumps alowed
      
    def calculateLeaps(n):
        if n == 0 or n == 1:
            return 1;
        else:
            leaps = 0;
            for i in range(0,n):
                leaps = leaps + calculateLeaps(i);
            return leaps;
      
    # Driver code
    print(calculateLeaps(4));
      
    # This code is contributed by mits


    C#
    // C# program to count total number of ways
    // to reach n-th stair with all jumps alowed
    using System;
      
    class GFG {
      
        // Function to calculate leaps
        static int calculateLeaps(int n)
        {
            if (n == 0 || n == 1) {
                return 1;
            }
            else {
                int leaps = 0;
                for (int i = 0; i < n; i++)
                    leaps += calculateLeaps(i);
                return leaps;
            }
        }
      
        // Driver code
        public static void Main()
        {
            Console.WriteLine(calculateLeaps(4));
        }
    }
      
    // This code is contributed by vt_m.


    PHP


    C++
    // C++ program to count total number of ways
    // to reach n-th stair with all jumps alowed
    #include 
      
    int calculateLeaps(int n)
    {
        if (n == 0)
            return 1;
        return (1 << (n - 1));
    }
      
    // Driver code
    int main()
    {
        int calculateLeaps(int);
        std::cout << calculateLeaps(4) << std::endl;
        return 0;
    }


    Java
    // Java program to count total number of ways
    // to reach n-th stair with all jumps alowed
    class GFG {
        static int calculateLeaps(int n)
        {
            if (n == 0)
                return 1;
            return (1 << (n - 1));
        }
      
        // Driver code
        public static void main(String[] args)
        {
            System.out.println(calculateLeaps(4));
        }
    }
    // This code is contributed by Anant Agarwal.


    Python3
    # python3 program to count
    # total number of ways
    # to reach n-th stair with
    # all jumps alowed
      
    def calculateLeaps(n):
        if (n == 0):
            return 1;
        return (1 << (n - 1));
      
    # Driver code
    print(calculateLeaps(4));
      
    # This code is contributed
    # by mits


    C#
    // C# program to count total number of ways
    // to reach n-th stair with all jumps alowed
    using System;
      
    class GFG {
      
        // Function to calculate leaps
        static int calculateLeaps(int n)
        {
            if (n == 0)
                return 1;
            return (1 << (n - 1));
        }
      
        // Driver code
        public static void Main()
        {
            Console.WriteLine(calculateLeaps(4));
        }
    }
      
    // This code is contributed by vt_m.


    PHP


    输出:

    8
    

    通过使用动态编程可以改善上述解决方案

  3. 让我们将此问题分解为一些小问题。猴子必须踩到最后一步,前N-1个步骤是可选的。猴子可以先踩0步,然后再踩到最高步,这是最大的跳跃。或者,它可以决定仅在两者之间踩一次,这可以通过n-1种方式[ (N-1) C 1 ]实现。依此类推,在以(N-1) C 2方式到达顶部之前,它只能踩2步。放在一起..

    F(N)= (N-1) C 0 + (N-1) C 1 + (N-1) C 2 + … + (N-1) C (N-2) + (N-1) C (N-1)
    这是二项式系数之和。
    = 2 ^(n-1)

C++

// C++ program to count total number of ways
// to reach n-th stair with all jumps alowed
#include 
  
int calculateLeaps(int n)
{
    if (n == 0)
        return 1;
    return (1 << (n - 1));
}
  
// Driver code
int main()
{
    int calculateLeaps(int);
    std::cout << calculateLeaps(4) << std::endl;
    return 0;
}

Java

// Java program to count total number of ways
// to reach n-th stair with all jumps alowed
class GFG {
    static int calculateLeaps(int n)
    {
        if (n == 0)
            return 1;
        return (1 << (n - 1));
    }
  
    // Driver code
    public static void main(String[] args)
    {
        System.out.println(calculateLeaps(4));
    }
}
// This code is contributed by Anant Agarwal.

Python3

# python3 program to count
# total number of ways
# to reach n-th stair with
# all jumps alowed
  
def calculateLeaps(n):
    if (n == 0):
        return 1;
    return (1 << (n - 1));
  
# Driver code
print(calculateLeaps(4));
  
# This code is contributed
# by mits

C#

// C# program to count total number of ways
// to reach n-th stair with all jumps alowed
using System;
  
class GFG {
  
    // Function to calculate leaps
    static int calculateLeaps(int n)
    {
        if (n == 0)
            return 1;
        return (1 << (n - 1));
    }
  
    // Driver code
    public static void Main()
    {
        Console.WriteLine(calculateLeaps(4));
    }
}
  
// This code is contributed by vt_m.

的PHP


输出:

8