【问题标题】:Recursion function to calculate the sum of squares in a sequence (and output the process)递归函数计算序列中的平方和(并输出过程)
【发布时间】:2020-06-11 02:53:51
【问题描述】:
int SeriesToOne(int n) {
   if (n == 1) {
       cout << "(" << 1 << "*" << 1 << ") = ";
       return 1;
   }
   cout << "(" << n << "*" << n << ") + ";
return (n * n) + SeriesToOne(n - 1); }

嘿,所以我正在编写一个程序,该程序应该使用递归计算序列中的平方和。

我正在编写两个函数,一个计算它从 1 到 N,另一个计算它从 N 到 1,并输出过程。上面的代码是我写的 N 到 1 的函数,但是我从 1 到 N 时遇到了很多麻烦。

我不知道如何在不向函数添加第二个参数(赋值指定一个参数)的情况下正确写出基本情况并达到基本情况。

如果有人可以帮助我,那就太棒了!对不起,如果我没有格式化这篇文章或把它放在错误的部分,第一次发帖。

【问题讨论】:

  • 这里没有尾递归(所以标签不合适)。但这实际上对您有好处:为什么不在打印之前进行递归调用?
  • 在打印最后一行之前?我不需要更改基本情况吗?

标签: c++ recursion tail-recursion


【解决方案1】:

你拥有的是

  • 完成这项工作(打印n 术语),然后完成剩余工作(从n-1 到1)

而你想要的是

  • 先做初始工作(从1 到n-1),然后再做这项工作(打印n 术语)

将递归的结果保存在一个变量中(并将= 位留给负责打印结果的main):

int SeriesToOne(int n) {
    if (n == 1) {
        cout << "(" << 1 << "*" << 1 << ")";
        return 1;
    }
    cout << "(" << n << "*" << n << ") + ";
    int sum = (n * n) + SeriesToOne(n - 1);
    return sum;
}

然后您可以更改工单(并移动+ 输出):

int SeriesFromOne(int n) {
    if (n == 1) {
        cout << "(" << 1 << "*" << 1 << ")";
        return 1;
    }
    int sum = (n * n) + SeriesFromOne(n - 1);
    cout << " + (" << n << "*" << n << ")";
    return sum;
}

【讨论】:

    【解决方案2】:

    我支持你!只是您需要在单个帧中进行尾部和头部递归。而已。 :D

    可以使用以下代码sn-p:

    int SeriesToOne(int n) {
      if (n == 0) return 0;
    
       cout<<endl<<n+" : "+(n * n);
    
    
       int sum = 0;
       sum += n * n;
       sum += seriesToOne(n-1); // due to loose overhead when n will become 0.
    
       cout<<endl<<n+" : "+(n * n);
    
       sum += n * n;
    
       return sum; // return the total sum we get after two functionality (tail-head recur.)
      }
    

    编辑:我不是 c++ 的朋友,如果有任何语法错误,请忽略所有语法错误!

    【讨论】:

      【解决方案3】:

      对于递归函数来说,这不是一个好的实现,但它可以完成这项工作:

      int SeriesToN(int n) {
          static int N = n;
          static bool firstTime = true;
          if (n == N && !(firstTime)) {
              cout << "(" << n << "*" << n << ") = ";
              return (n*n);
          }
          else {
              if (n == N) {
                  firstTime = false;
                  n = 1;
              }
              cout << "(" << n << "*" << n << ") + ";
              return (n * n) + SeriesToN(n + 1);
          }
      }
      

      如你所愿,它从 1 计数到 n,只有一个参数,没有任何全局变量。

      【讨论】:

        【解决方案4】:
        #include <iostream>
        #include <string>
        
        using namespace std;
        
        int SeriesToN(int n, int limit = -1) 
        {   
            // catch edge cases
            if (n == 1) {
                cout << "(1 * 1) = ";
                return 1;
            } else if (n <= 0) {
                cout << "n should be >= ";
                return 1;
            }
        
            // the actual action
            if (limit == 0) 
            {
                cout << "(" << n << "*" << n << ") = ";
                return n * n;
            } else if (limit == -1) // handling one argument input
            {
                limit = n - 1;
                n = 1;
                cout << "(" << 1 << "*" << 1 << ") + ";
                return (1) + SeriesToN(2, limit - 1);         
            } else
            {   
                cout << "(" << n << "*" << n << ") + ";
                return (n * n) + SeriesToN(n + 1, limit - 1); 
            }
        };
        
        
        int main()
        {
            cout << SeriesToN(10);
            cout << "\n";
            cout << SeriesToN(3);
            cout << "\n";
            cout << SeriesToN(2);
            cout << "\n";
            cout << SeriesToN(1);
            cout << "\n";
            cout << SeriesToN(0);
            cout << "\n";
            cout << SeriesToN(-1);
            cout << "\n";
            return 0;
        }
        
        
        /* output:
        (1*1) + (2*2) + (3*3) + (4*4) + (5*5) + (6*6) + (7*7) + (8*8) + (9*9) + (10*10) = 385
        (1*1) + (2*2) + (3*3) = 14
        (1*1) + (2*2) = 5
        (1 * 1) = 1
        n should be >= 1
        n should be >= 1
        */
        

        【讨论】:

          【解决方案5】:

          这是在 javascript 中的实现。

          function SeriesToN(n, initialCount){
                 if(!initialCount){
                          initialCount  = 1;
                        }
                 if(initialCount === n){
                      return n * n ;
                   }
          
                  return (initialCount * initialCount) + SeriesToN(n , initialCount + 1)
          
               }
          
               SeriesToN(5)
          

          【讨论】:

            【解决方案6】:

            好的,由于基本条件,您需要知道递归函数中 n 的值,您可以将变量声明为全局变量,也可以在每次调用中将其传递给函数,如下所示:

            int SeriesToN(int n, int N) {
                if (n == N) {
                    cout << "(" << n << "*" << n << ") = ";
                    return n * n;
                }
                cout << "(" << n << "*" << n << ") + ";
                return (n * n) + SeriesToN(n + 1, N); 
            }
            

            如果n = 4,则调用如下函数:

            SeriesToN(1, 4);
            

            这应该可以解决您的问题。编码愉快!

            【讨论】:

            • 欣赏!非常感谢。有没有办法用一个函数一个参数(不使用全局变量)?
            • 我觉得没有别的办法了。
            • 我用(实际上)一个论点发布了一种方法。但确实,在这种情况下需要两个。
            猜你喜欢
            • 1970-01-01
            • 2013-11-26
            • 1970-01-01
            • 2017-10-09
            • 1970-01-01
            • 1970-01-01
            • 2017-04-16
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多