简体   繁体   English

递归重写一个简单的迭代算法

[英]Rewrite a simple iterative algorithm recursively

It is known that in some cases an iteration can be transformed in a recursive algorithm. 众所周知,在某些情况下,可以在递归算法中转换迭代。 How can I rewrite an iteration as simple as the following one as a recursion? 我怎样才能像下一个一样简单地重写一个迭代?

for(i=0,i<500,i++) 
    row_multiply();

I realize that, as it has been pointed out, I have to try something like... 我意识到,正如已经指出的那样,我必须尝试类似...

void recursiveSolution(int i)

    {
        raw_multiply();
        if (i< 499)
            recursiveSolution(i+ 1);
    }

..., but I am unsure about how to deal with the base case and how to structure coherent C code for the recursive rewrite. ...,但是我不确定如何处理基本情况以及如何为递归重写构造一致的C代码。

May be this way? 可能是这样吗?

void recursiveSolution(int count)
{
    raw_multiply();
    if (count < 499)
        recursiveSolution(count + 1);
}

and then to start it 然后开始

recursiveSolution(0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM