青蛙跳步问题是一个经典的编程问题,可以使用动态规划来解决。问题陈述如下:
青蛙一次只能走 1 步、2 步或 3 步才能到达楼梯顶部。考虑到楼梯上的台阶总数,我们需要确定青蛙可以到达顶部的不同方式的数量。
以下是 C 语言中青蛙跳跃步骤问题的示例实现:
'''c
#include <stdio.h>
int countDistinctWays(int numSteps) {
int dp[numSteps + 1];
dp[0] = 1; // Base case: There is one way to reach step 0 (by not taking any steps)
// Calculate the number of ways for each step up to numSteps
for (int i = 1; i <= numSteps; i++) {
dp[i] = 0;
// Add the number of ways from the previous three steps (1, 2, and 3)
if (i >= 1)
dp[i] += dp[i - 1];
if (i >= 2)
dp[i] += dp[i - 2];
if (i >= 3)
dp[i] += dp[i - 3];</