简体   繁体   English

如何将循环转换为递归?

[英]How do I convert loops to recursions?

I'm not quite understanding recursions. 我不太了解递归。 I'm working on a class program, and I converted most of my program to recursions. 我正在开发一个类程序,并且将大部分程序转换为递归。 So, my professor doesn't want us to use loops and also doesn't want us to use more than one parameter. 因此,我的教授不希望我们使用循环,也不想我们使用多个参数。

The program runs through the hailstone sequence and the function I'm having trouble computes the length of the hailstone sequence. 该程序遍历冰雹序列,而我遇到的函数无法计算冰雹序列的长度。 I had it working my way, but wanted to confirm if it was up to his standards. 我按照自己的方式进行了工作,但想确认它是否符合他的标准。 (Is not to his standards.) (不符合他的标准。)

I wanted to know if there was a better way I could do this function. 我想知道是否有更好的方法可以执行此功能。 The problem I'm having when I tried was I can't use a static variable as it would never reset the counter and would total the hailstone sequences from 1 to n. 我尝试时遇到的问题是我无法使用静态变量,因为它永远不会重置计数器,并且将冰雹序列从1改为n。 I've also tried doing it by leaving the count in my function as was, though when I did hand sims (since I refused to believe it reset more than I wanted) and saw it reset everytime. 我也尝试过按原样保留计数,但是当我做手模拟游戏时(因为我拒绝相信重置次数超过了我的期望),并且每次都看到重置。 Also, no answer is needed. 另外,不需要答案。

I would rather figure it on my own, so I was asking to see if anyone could give me a "formula" or some guide to follow to convert loops to recursions. 我宁愿自己想一想,所以我想问问是否有人可以给我“公式”或一些指导,以遵循将循环转换为递归的方式。 As reference I'll paste my hailstoneLength code that finds the length of the hailstone sequence. 作为参考,我将粘贴可以找到冰雹序列长度的hailstoneLength代码。 Though, if you would rather tell me how to do it is it okay if you could explain how it works? 但是,如果您想告诉我该怎么做,可以解释一下它如何工作吗? I'll most likely do some hand simulations to work it out. 我很可能会做一些手工模拟来解决。

Loop: 环:

int hailstoneLength(int n)
{
    int count = 1;
    int u = n;
    while(u != 1)
    {
        u = hailNext(u);
        count++;
    }
    return count;
}

Recursion: 递归:

int hailstoneLength(int n, int count = 1) 
{
    int u = n;
    if(u != 1)
    {
        return hailstoneLength(hailNext(u), count + 1);
    }
    else // if u = 1
    {
        return count;
    }
}

I was thinking I needed to make some function that works the count since it cant be a parameter, or in the function without resetting. 我当时想我需要制作一些可以计数的函数,因为它不能成为参数,或者需要在函数中不进行复位。

Thanks 谢谢

First of all, you have confused the picture by introducing u as an extraneous variable. 首先,您通过引入u作为无关紧要的变量而使图片感到困惑。 The non-recursive function can be simplified to 非递归函数可以简化为

int hailstoneLength(int n)
{
    int count = 1;
    while(n != 1)
    {
        n = hailNext(n);
        count++;
    }
    return count;
}

From this it is easy to produce a recursive form 由此很容易产生递归形式

int hailstoneLength(int n)
{
    if (n != 1)
        return hailstoneLength(hailNext(n)) + 1;
    else
        return 1;
}

which can be recast to make it clear recursion stops when n == 1 . n == 1时,可以重铸它以使其清楚递归停止。

int hailstoneLength(int n)
{
    if (n == 1)
        return 1;
    else
        return hailstoneLength(hailNext(n)) + 1;
}

or even (to make it obvious this can be written in a tail recursive form) .... 甚至(很明显,这可以用尾递归形式编写)...。

int hailstoneLength(int n)
{
    if (n == 1) return 1;
    return hailstoneLength(hailNext(n)) + 1;
}

This would do what you need. 这将满足您的需求。 You don't need a tail-recursive function to keep track of the amount of times the function is recurses, which is essentially what you're keeping track of here. 您不需要尾部递归函数来跟踪该函数递归的次数,这实际上就是您要跟踪的地方。

int hailstoneLength(int n) 
{
    int u = n;
    if(u != 1)
    {
        return 1 + hailstoneLength(hailNext(u));
    }
    else // if u = 1
    {
        return 1;
    }
}

There's no real general formula for breaking down iterative loops to recursive function calls, but eliminating the loop invariant as we're doing here is the general idea. 没有真正的通用公式可以将迭代循环分解为递归函数调用,但是在此处进行操作时消除循环不变性是一个基本思路。

You've defined your return type as an int, but hailstoneLength(hailNext(u), count + 1) does not seem to be an int. 您已将返回类型定义为int,但是hailstoneLength(hailNext(u),count + 1) 似乎不是int。

Instead, what you could do, is return hailstoneLength(hailNext(u+1)), so that on the next loop iteration it will call hailstoneLength(n+1) (I believe that is the general principle behind what you're trying to do). 相反,您可以做的是返回hailstoneLength(hailNext(u + 1)),以便在下一个循环迭代中将调用hailstoneLength(n + 1)(我相信这是您尝试执行的操作的一般原理)做)。 For what it's worth, I don't think you need int u = n. 对于它的价值,我认为您不需要int = n。

I have no idea what hailNext does, so I can't help further, but you definitely want to make sure you're returning an int in recursive functions (which generally means you can't have multiple inputs). 我不知道hailNext会做什么,所以我无济于事,但是您绝对想确保您要在递归函数中返回int(这通常意味着您不能有多个输入)。

Take a look at this recursive function for doing a factorial, for reference: 请看一下用于做阶乘的递归函数,以供参考:

int myFactorial( int integer)
{
if( integer == 1)
    return 1;
else
   {
   return (integer * (myFactorial(integer-1)));
   }
}

Suppose integer is 5. 假设整数是5。

It returns (5 * (myFactorial(integer-1))) 返回(5 *(myFactorial(integer-1)))

So on the next iteration, integer is 4. 因此,在下一次迭代中,整数为4。

So it will return 4 * (myFactorial(integer-1))) 因此它将返回4 *(myFactorial(integer-1)))

And so on! 等等! At the end it will return 5 * 4 * 3 * 2 * 1. 最后它将返回5 * 4 * 3 * 2 * 1。

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

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