📌  相关文章
📜  采取1步和2步,以及正好3步的方法,计算到达第N个楼梯的方式

📅  最后修改于: 2021-05-06 17:56:05             🧑  作者: Mango

给定代表楼梯数量的数字N ,任务是通过任意次数采取1步,2步并精确地执行3步一次来到达第N楼梯。
例子:

方法:可以使用动态编程解决此问题。到达N楼梯的人应该是对(N – 1),(N – 2)或(N – 3)。所以,为了达到n的基座覆盖率阶梯(N – 1)阶梯,其包括精确只有3一个步骤,前移(N – 2)步骤与以下步骤其中inclues正好一个步骤3,到达(N – 3)而不采取的任何3步骤的一步。
因此,针对此问题的递归关系将是–

反之,当一次不允许3步数时的递归关系为

下面是上述方法的实现。

C++
// C++ implementation to find the number
// the number of ways to reach Nth stair
// by taking 1, 2 step at a time and
// 3 Steps at a time exactly once.
 
#include 
using namespace std;
 
// Function to find the number
// the number of ways to reach Nth stair
int number_of_ways(int n)
{
    // Array including number
    // of ways that includes 3
    int includes_3[n + 1] = {};
 
    // Array including number of
    // ways that doesn't includes 3
    int not_includes_3[n + 1] = {};
 
    // Intially to reach 3 stairs by
    // taking 3 steps can be
    // reached by 1 way
    includes_3[3] = 1;
 
    not_includes_3[1] = 1;
    not_includes_3[2] = 2;
    not_includes_3[3] = 3;
 
    // Loop to find the number
    // the number of ways to reach Nth stair
    for (int i = 4; i <= n; i++) {
        includes_3[i]
            = includes_3[i - 1] + includes_3[i - 2] + not_includes_3[i - 3];
        not_includes_3[i]
            = not_includes_3[i - 1] + not_includes_3[i - 2];
    }
    return includes_3[n];
}
 
// Driver Code
int main()
{
    int n = 7;
 
    cout << number_of_ways(n);
 
    return 0;
}


Java
// Java implementation to find the number
// the number of ways to reach Nth stair
// by taking 1, 2 step at a time and
// 3 Steps at a time exactly once.
class GFG
{
 
// Function to find the number
// the number of ways to reach Nth stair
static int number_of_ways(int n)
{
    // Array including number
    // of ways that includes 3
    int []includes_3 = new int[n + 1];
     
    // Array including number of
    // ways that doesn't includes 3
    int []not_includes_3 = new int[n + 1];
 
    // Intially to reach 3 stairs by
    // taking 3 steps can be
    // reached by 1 way
    includes_3[3] = 1;
 
    not_includes_3[1] = 1;
    not_includes_3[2] = 2;
    not_includes_3[3] = 3;
 
    // Loop to find the number
    // the number of ways to reach Nth stair
    for (int i = 4; i <= n; i++)
    {
        includes_3[i]
            = includes_3[i - 1] + includes_3[i - 2] +
               not_includes_3[i - 3];
        not_includes_3[i]
            = not_includes_3[i - 1] + not_includes_3[i - 2];
    }
    return includes_3[n];
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 7;
 
    System.out.print(number_of_ways(n));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation to find the number
# the number of ways to reach Nth stair
# by taking 1, 2 step at a time and
# 3 Steps at a time exactly once.
 
# Function to find the number
# the number of ways to reach Nth stair
def number_of_ways(n):
     
    # Array including number
    # of ways that includes 3
    includes_3 = [0]*(n + 1)
 
    # Array including number of
    # ways that doesn't includes 3
    not_includes_3 = [0] * (n + 1)
 
    # Intially to reach 3 stairs by
    # taking 3 steps can be
    # reached by 1 way
    includes_3[3] = 1
 
    not_includes_3[1] = 1
    not_includes_3[2] = 2
    not_includes_3[3] = 3
 
    # Loop to find the number
    # the number of ways to reach Nth stair
    for i in range(4, n + 1):
        includes_3[i] = includes_3[i - 1] + \
                        includes_3[i - 2] + \
                        not_includes_3[i - 3]
        not_includes_3[i] = not_includes_3[i - 1] + \
                           not_includes_3[i - 2]
    return includes_3[n]
 
# Driver Code
n = 7
 
print(number_of_ways(n))
 
# This code is contributed by mohit kumar 29


C#
// C# implementation to find the number
// the number of ways to reach Nth stair
// by taking 1, 2 step at a time and
// 3 Steps at a time exactly once.
using System;
 
class GFG
{
 
// Function to find the number
// the number of ways to reach Nth stair
static int number_of_ways(int n)
{
    // Array including number
    // of ways that includes 3
    int []includes_3 = new int[n + 1];
     
    // Array including number of
    // ways that doesn't includes 3
    int []not_includes_3 = new int[n + 1];
 
    // Intially to reach 3 stairs by
    // taking 3 steps can be
    // reached by 1 way
    includes_3[3] = 1;
 
    not_includes_3[1] = 1;
    not_includes_3[2] = 2;
    not_includes_3[3] = 3;
 
    // Loop to find the number
    // the number of ways to reach Nth stair
    for (int i = 4; i <= n; i++)
    {
        includes_3[i]
            = includes_3[i - 1] + includes_3[i - 2] +
            not_includes_3[i - 3];
        not_includes_3[i]
            = not_includes_3[i - 1] + not_includes_3[i - 2];
    }
    return includes_3[n];
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 7;
 
    Console.Write(number_of_ways(n));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
20

相似的文章:计算一次使用1、2或3步到达Nth楼梯的方法