📌  相关文章
📜  使用多个1或2个步骤以及单个3个步骤,计算到达第N个楼梯的方式

📅  最后修改于: 2021-05-20 06:10:42             🧑  作者: Mango

给定的整数N个台阶的,任务是计数数量的方式通过取1或2步骤任意次数,但服用3步骤一次以到达N楼梯。
例子:

方法:可以使用置换和组合的概念来解决此问题。存在一个约束,根据该约束,一次必须精确执行一次三个步骤。这个想法是要计算可能要采取的3个步骤的职位数量。
现在,下一个任务是找到移动到N楼梯的可能的两个步骤的数量,即(N – 3)/2。这样,如果计数为两个,则所有可能的方法都将被覆盖。步骤一次增加一次,并且对于每个步骤,将计算所有可能的组合。
在概括了步骤之后,每个可能序列的可能组合将是

Ways = (Length of sequence)! * (Count of 2-step)! 
       ------------------------------------------
                  (Count of 1-step)! 

算法:

  • 将序列的长度初始化为(N – 2)。
  • 将1步的计数初始化为(N – 3)。
  • 将2步计数初始化为0
  • 将可能的2步计数初始化为(N-3)/ 2。
  • 循环运行,直到可能的2步计数等于2步的实际计数。
    • 借助当前序列的长度,1步计数,2步计数以及上述公式,找到可能的方法数量。
    • 将序列的长度减少1。
    • 将2步计数增加1

下面是上述方法的实现。

C++
// C++ implementation to find the number
// the number of ways to reach Nth stair
// by taking 1 or 2 steps at a time and
// 3rd step exactly once
#include 
using namespace std;
 
int factorial(int n)
{
    // Single line to find factorial
    return (n == 1 || n == 0) ? 1 :
            n * factorial(n - 1);
}
 
// Function to find
// the number of ways
int ways(int n)
{
    // Base Case
    if (n < 3)
    {
        return 0;
    }
 
    // Count of 2-steps
    int c2 = 0;
 
    // Count of 1-steps
    int c1 = n - 3;
 
    // Intial length of sequence
    int l = c1 + 1;
    int s = 0;
 
    // Expected count of 2-steps
    int exp_c2 = c1 / 2;
     
    // Loop to find the ways for
    // every possible sequence
    while (exp_c2 >= c2)
    {
        int f1 = factorial(l);
        int f2 = factorial(c1);
        int f3 = factorial(c2);
        int f4 = (f2 * f3);
 
        s += f1 / f4;
        c2 += 1;
        c1 -= 2;
        l -= 1;
    }
    return s;
}
 
// Driver code
int main()
{
    int n = 7;
    int ans = ways(n);
     
    cout << ans << endl;
    return 0;
}
 
// This code is contributed by coder001


Java
// Java implementation to find the number
// the number of ways to reach Nth stair
// by taking 1 or 2 steps at a time and
// 3rd step exactly once
class GFG {
 
static int factorial(int n)
{
     
    // Single line to find factorial
    return (n == 1 || n == 0) ? 1 :
            n * factorial(n - 1);
}
 
// Function to find
// the number of ways
static int ways(int n)
{
     
    // Base Case
    if (n < 3)
    {
        return 0;
    }
 
    // Count of 2-steps
    int c2 = 0;
 
    // Count of 1-steps
    int c1 = n - 3;
 
    // Intial length of sequence
    int l = c1 + 1;
    int s = 0;
 
    // Expected count of 2-steps
    int exp_c2 = c1 / 2;
 
    // Loop to find the ways for
    // every possible sequence
    while (exp_c2 >= c2)
    {
        int f1 = factorial(l);
        int f2 = factorial(c1);
        int f3 = factorial(c2);
        int f4 = (f2 * f3);
         
        s += f1 / f4;
        c2 += 1;
        c1 -= 2;
         l -= 1;
    }
    return s;
}
 
// Driver Code
public static void main(String args[])
{
    int n = 7;
    int ans = ways(n);
     
    System.out.println(ans);
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 implementation to find the number
# the number of ways to reach Nth stair
# by taking 1 or 2 steps at a time and
# 3rd Step exactly once
 
import math
 
# Function to find
# the number of ways
def ways(n):
     
    # Base Case
    if n < 3:
        return 0
 
    # Count of 2-steps
    c2 = 0
 
    # Count of 1-steps
    c1 = n-3
 
    # Intial length of sequence
    l = c1 + 1
    s = 0
 
    # expected count of 2-steps
    exp_c2 = c1 / 2
     
    # Loop to find the ways for
    # every possible sequence
    while exp_c2 >= c2:
        f1 = math.factorial(l)
        f2 = math.factorial(c1)
        f3 = math.factorial(c2)
        s += f1//(f2 * f3)
        c2 += 1
        c1 -= 2
        l -= 1
    return s
 
# Driver Code
if __name__ == "__main__":
    N = 7
    print(ways(N))


C#
// C# implementation to find the number
// the number of ways to reach Nth stair
// by taking 1 or 2 steps at a time and
// 3rd step exactly once
using System;
class GFG{
     
static int factorial(int n)
{
         
    // Single line to find factorial
    return (n == 1 || n == 0) ? 1 :
            n * factorial(n - 1);
}
     
// Function to find
// the number of ways
static int ways(int n)
{
         
    // Base Case
    if (n < 3)
    {
        return 0;
    }
     
    // Count of 2-steps
    int c2 = 0;
     
    // Count of 1-steps
    int c1 = n - 3;
     
    // Intial length of sequence
    int l = c1 + 1;
    int s = 0;
     
    // Expected count of 2-steps
    int exp_c2 = c1 / 2;
     
    // Loop to find the ways for
    // every possible sequence
    while (exp_c2 >= c2)
    {
        int f1 = factorial(l);
        int f2 = factorial(c1);
        int f3 = factorial(c2);
        int f4 = (f2 * f3);
             
        s += f1 / f4;
        c2 += 1;
        c1 -= 2;
        l -= 1;
    }
    return s;
}
 
// Driver code
static void Main()
{
    int n = 7;
    int ans = ways(n);
         
    Console.WriteLine(ans);
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
20

性能分析:

  • 时间复杂度:与上述方法一样,有一个循环来检查每个序列的可能排列,直到可能花费O(N)的上至(N-3)为止,并找出可能要花费O(N)的组合,因此时间复杂度将为O(N 2 )
  • 空间复杂度:与上述方法一样,没有使用额外的空间,因此空间复杂度将为O(1)

注意:通过预先计算直到N的每个数字的阶乘,可以将时间复杂度提高到O(N)。

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。