【问题标题】:Recursion assignment not giving correct output递归分配没有给出正确的输出
【发布时间】:2015-11-23 08:36:44
【问题描述】:

我有一个任务,我们正在执行 a + b 递归操作。如果这是一个超级简单的解决方法,请多多包涵,因为我是新手。最近我犯了一些简单的错误,只是没有看到它们。

我们必须使用:

BASE CASE:   if ( a == 0 ) return( b ); BASE CASE:   if ( b == 0 ) return( a ); 
RECURSIVE CASE:  else  return( RecursiveAPlusB( a - 1, b - 1 ) + 2 );
  1. 我的标题是:

    #ifndef Adder_h
    #define Adder_h
    #include <iostream>
    using namespace std;
    
    class Adder{
    public:
       Adder( int a, int b );
       int getA( ) const;
       int getB( ) const;
       int RecursiveAPlusB( ) const;
       int IterativeAPlusB( ) const;
    private:
       int myValueofA;
       int myValueofB;
    
    };
    
    #endif /* Adder_h */
    
  2. 我的司机是:

    #include "Adder.h"
    
    Adder::Adder(int a, int b){
        myValueofA = a;
        myValueofB = b;
        }
    
    int Adder::getA( ) const{
        return myValueofA;}
    
    int Adder::getB() const{
        return myValueofB;
    }
    
    int Adder::IterativeAPlusB( ) const{
        if(myValueofA==0)
            return myValueofB;
        else if(myValueofB==0)
            return myValueofA;
        else
            return RecursiveAPlusB();
    }
    
    int Adder::RecursiveAPlusB() const{
        return ((myValueofA - 1, myValueofB - 1 ) + 2) ;
    }
    
  3. 我的主要是(作业要求的):

    #include "Adder.h"
    #include <iostream>
    
    using namespace std;
    
    int main() {
       Adder ten( 6, 4 );
    
      // All these calls should produce the
    
      // exact same answer...
    
      // namely, the number 10!
    
      cout << ten.RecursiveAPlusB( ) << endl;
    
      cout << ten.IterativeAPlusB( ) << endl;
    
      cout << ten.RecursiveAPlusB( ) << endl;
    
      Adder tenagain( 2, 8 );
    
      cout << tenagain.RecursiveAPlusB( ) << endl;
    
      cout << tenagain.IterativeAPlusB( ) << endl;
    
      cout << tenagain.RecursiveAPlusB( ) << endl;
    return 0;
    

    }

请告诉我我的数学哪里出错了?!输出是 5 5 5 9 9 9,但应该都是 10。谢谢!!!

【问题讨论】:

  • Recursive 是什么意思?在您的 RecursiveAPlusB() 中没有递归。
  • 我的教授。给了我他想要的递归公式,我只是在使用它,所以我不确定......

标签: c++ recursion iteration


【解决方案1】:

在我看来,根据作业的描述,您应该按照以下方式定义功能。首先你需要在类中再添加一个成员函数

#ifndef Adder_h
#define Adder_h
#include <iostream>
using namespace std;

class Adder{
public:
   Adder( int a, int b );
   int getA( ) const;
   int getB( ) const;
   int RecursiveAPlusB( ) const;
   int IterativeAPlusB( ) const;

private:
   int RecursiveAPlusB( int a, int b ) const;

private:
   int myValueofA;
   int myValueofB;

};

#endif /* Adder_h */

函数定义看起来像

int Adder::RecursiveAPlusB() const
{
    return RecursiveAPlusB( getA(), getB() );
}

int Adder::RecursiveAPlusB( int a, int b ) const
{
    if ( a == 0 ) return b;
    else if ( b == 0 ) return a;
    else return  2 + RecursiveAPlusB( a - 1, b - 1 ) ;
}

int Adder::IterativeAPlusB( ) const{
{
    int sum = 0;
    int a = getA(), b = getB();

    for ( ; a != 0 && b != 0; a -= 1, b -=1 )
    {
        sum += 2;
    }

    if ( a == 0 ) sum += b;
    else sum += a;

    return sum;
}    

考虑到函数仅在值为非负数的情况下才有效。因此最好将它们声明为具有unsigned int 类型。

【讨论】:

  • 非常感谢!我没有意识到我可以创建一个私有递归函数,所以现在我会记住这一点。再次感谢。
  • @brie 我在迭代函数中打错了,但我已经更新了。:)
【解决方案2】:

在你的函数中你使用comma operator:

int Adder::RecursiveAPlusB() const{
    return ((myValueofA - 1, myValueofB - 1 ) + 2) ;
}

(6 - 1, 4 - 1) = 3 的结果。

【讨论】:

  • 在作业中,我的教授给了我们特殊情况,而逗号运算符只是其中的一部分,所以我迷路了。我不知道它是如何工作的,但那会是 (8) + 2 吗?
【解决方案3】:

一种方式 - 将 RecursiveAPlusB() 更改为:

int Adder::RecursiveAPlusB() const
{
    return (Adder(myValueofA - 1, myValueofB - 1 ).IterativeAPlusB() + 2) ;
}

【讨论】:

  • 哇,解决了这个问题!但我必须使用我的教授的特殊情况。分配给我,所以我不知道我是否可以使用它:-(,但也许我被允许谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-22
  • 1970-01-01
  • 1970-01-01
  • 2019-09-14
  • 2013-07-30
相关资源
最近更新 更多