给定一个表示楼梯数量的数字N ,任务是通过走 1、2 步任意次数到达第N个楼梯,并且只走 3 步一次。
例子:
Input: N = 4
Output: 2
Explanation:
Since a step of 3 has to be taken compulsorily and only once,
There are only two possible ways: (1, 3) or (3, 1)
Input: N = 5
Output: 5
Explanation:
Since a step of 3 has to be taken compulsorily and only once,
There are only 5 possible ways:
(1, 1, 3), (1, 3, 1), (3, 1, 1), (2, 3), (3, 2)
方法:这个问题可以用动态规划解决。要到达第N个楼梯,该人应位于 (N – 1) th 、(N – 2) th或 (N – 3) th 。所以,为了达到n的底座延伸(N – 1)个楼梯个楼梯,其包括精确只的3一个步骤,前移(N – 2)与步骤个步骤,其中包括的3正好一个步骤并到达(N – 3)第3 步,不采取任何 3 步。
所以,这个问题的递归关系将是——
includes_3[i] = includes_3[i-1] + includes_3[i-2] + not_includes[i-3]
而不允许一次执行 3 个步骤时的递归关系为
not_includes[i] = not_includes[i – 1] + not_includes[i – 2]
下面是上述方法的实现。
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] = {};
// Initially 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];
// Initially 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)
# Initially 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];
// Initially 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 步到达第 N 级楼梯的方法
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。