【问题标题】:Number Pattern Function in c++ using recursionc++中使用递归的数字模式函数
【发布时间】:2015-10-25 19:55:44
【问题描述】:

使用递归实现以下功能。不要使用任何局部变量或循环。

无效模式(无符号整数)

// 前置条件:n > 0;

// 后置条件:输出由整数行组成。第一行

// 是数字 n。下一行是数字 2n。下一行是

// 数字 4n,以此类推,直到您达到大于大于的数字

// 4242. 然后这个数字列表向后重复,直到你回来

// 到 n.

/* n = 840 的示例输出:

840

1680

3360

6720

6720

3360

1680

840 */

//这是我的代码

#include <iostream>

using namespace std;

void pattern(unsigned int n)
{
    if(n > 0)
    {
        cout << n << endl;
        return pattern(n * 2);
    }else if( n > 4242)
    {
        return pattern(n / 2);
    }
}

int main()
{
    pattern(840);
    return 0;
}

//我的代码只是不断加倍n,并没有分回到原来的n。

【问题讨论】:

    标签: c++ recursion


    【解决方案1】:

    另外两个答案指出了其中一个问题(只要n &gt; 4242 为真,n &gt; 0 就为真),但另一个问题是,如果n &gt; 4242,您只能使用n / 2 调用pattern。所以你最终会来回“乒乓”。例如,在您在问题中显示的示例输出中,当您点击6720 时,您将调用pattern(3360) 时将其减半,但在下一次调用时,您将使用3360 调用模式,因为3360 &lt; 4242 加倍.

    我认为最明显的方法是将其拆分为两个函数并添加一个“方向”布尔值来指示您是向上还是向下:

    void pattern(unsigned int n) {
       pattern_with_dir(n, true);
    }
    
    void patten_with_dir(unsigned int n, bool increase) {
        if (n <= 0) {
            return;
        }
        cout << n << endl;
        if (n > 4242) {
            pattern_with_dir(n, false);
        } else {
            if (increase) {
                pattern_with_dir(n * 2, true);
            } else {
                pattern_with_dir(n / 2, false);
            }
        }
    }
    

    请注意,您也可以将其拆分为 3 个功能:

    void pattern(unsigned int n) {
       pattern_up(n);
    }
    
    void pattern_up(unsigned int n) {
        cout << n << endl;
        if (n > 4242) {
            pattern_down(n);
        } else {
           pattern_up(n * 2);
        }
    }
    
    void pattern_down(unsigned int n) {
        if (n <= 0) {
            return;
        }
        cout << n << endl;
        pattern_down(n / 2);
    }
    

    但最简洁的解决方案是利用递归堆栈来帮助您倒计时:

    void pattern(unsigned int n) {
       // Going up
       count << n << endl;
       if (n <= 4242) {
           pattern(n*2);
       }
       // this will be called "on the way back down"
       count << n << endl;
    }
    

    【讨论】:

    • 谢谢,非常感谢!
    • 当然。如果答案似乎正确,请将其标记为正确答案。
    • 你正在使用 pattern_with_dir(n / 2, false);如果您明智地使用递归,则根本不需要它。
    • @Nandu 好点。添加了另外两个选项来解决这个问题,包括更简洁的单功能版本。
    【解决方案2】:
    void pattern(unsigned int n)
    {
        if (n > 0)
        {
            cout << n << endl;
            if (n < 4242)
                pattern(n * 2);
            cout << n << endl;
        }
    }
    

    【讨论】:

    • n &lt; 4242 不是 n &gt; 4242 的反义词。只有n &lt;= 4242 是。
    • 谢谢。非常感谢。
    【解决方案3】:

    if( n &gt; 4242) 只有在(n &gt; 0) 不为真时才会被评估。但只要n &gt; 4242 为真,n &gt; 0 也将如此。

    【讨论】:

    • 谢谢,非常感谢
    【解决方案4】:

    它永远不会碰到 else if,因为第一个条件 (>0) 仍然为真,它会在第一个 if 语句中结束。

    制定第一个标准

    if(n > 0 && n <= 4242) {
    ...
    } else if(n > 4242){
    ...
    }
    

    或者反转 if 和 else if

    if(n > 4242) {
        return pattern(n/2);
    } else if (n >0) {
        return pattern(n*2);
    }
    

    【讨论】:

    • 在第一种情况下,如果n == 4242 会发生什么?
    【解决方案5】:
    // ConsoleApplication3.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    #include <iostream>
    
    using namespace std;
    
    void pattern(unsigned int n)
    {
        cout << n << endl;
        if (n > 4242) {
            cout << n << endl;
            return ;
        }
    
            pattern(n * 2);
    
        cout << n << endl;
    
        return ;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        pattern(840);
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-21
      • 2023-02-18
      • 2013-07-16
      相关资源
      最近更新 更多