【问题标题】:What is the Recursive Algorithm for this problem? [duplicate]这个问题的递归算法是什么? [复制]
【发布时间】:2021-02-19 05:11:00
【问题描述】:

一个机器人可以做出三种不同长度的台阶:1 厘米、2 厘米、3 厘米。编写一个递归算法来找出机器人可以通过距离“d”的不同方式的数量

【问题讨论】:

  • 你试过什么?您可以通过展示研究成果并将其阐明为特定问题来改进此问题,而不是简单地寻求解决方案。见How to ask
  • 这能回答你的问题吗? Recursive change-making algorithm

标签: algorithm recursion


【解决方案1】:

这将是递归算法

int findNumberOfWaysToTraverse(int d)
{
    if (d == 1 || d == 0) // Base case: If the value of d is less than 0 then return 0, and if the value of d is equal to zero then return 1 as it is the starting place.
        return 1;
    else if (d == 2)
        return 2;


    else
        return findNumberOfWaysToTraverse(d - 3) + //recursive calls
               findNumberOfWaysToTraverse(d - 2) + 
               findNumberOfWaysToTraverse(d - 1);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 2012-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    相关资源
    最近更新 更多