如何将循环转换为递归? [英] How do I convert loops to recursions?

查看:52
本文介绍了如何将循环转换为递归?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太了解递归.我正在编写一个类程序,并将大部分程序转换为递归.所以,我的教授不希望我们使用循环,也不希望我们使用多个参数.

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.)

我想知道是否有更好的方法可以实现此功能.我尝试时遇到的问题是我不能使用静态变量,因为它永远不会重置计数器并且会将冰雹序列从 1 加到 n.我也尝试通过将计数保留在我的函数中来做到这一点,尽管当我进行手动模拟时(因为我拒绝相信它重置的次数超过我想要的次数)并且每次都看到它重置.同样,不需要回答.

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. 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.

我宁愿自己计算,所以我想看看是否有人可以给我一个公式"或一些指导来遵循将循环转换为递归.作为参考,我将粘贴用于查找冰雹序列长度的 hailstoneLength 代码.不过,如果你更愿意告诉我怎么做,你能解释一下它是如何工作的吗?我很可能会做一些手工模拟来解决这个问题.

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. 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.

循环:

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

递归:

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.

谢谢

推荐答案

首先,您将 u 作为无关变量引入,从而混淆了图片.非递归函数可以简化为

First of all, you have confused the picture by introducing u as an extraneous variable. 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;
}

可以重铸以明确当n == 1时递归停止.

which can be recast to make it clear recursion stops when 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;
}

这篇关于如何将循环转换为递归?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆