Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 这个问题的递归算法是什么?_Algorithm_Recursion - Fatal编程技术网

Algorithm 这个问题的递归算法是什么?

Algorithm 这个问题的递归算法是什么?,algorithm,recursion,Algorithm,Recursion,机器人可以走三种不同长度的步:1厘米、2厘米、3厘米。编写一个递归算法,找出机器人可以通过的不同路径数“d”这将是递归算法 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 i

机器人可以走三种不同长度的步:1厘米、2厘米、3厘米。编写一个递归算法,找出机器人可以通过的不同路径数“d”

这将是递归算法

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);
}

你试过什么?您可以通过展示研究成果并将其澄清为具体问题来改进此问题,而不是简单地要求解决方案。看这回答了你的问题吗?