递归函数并通过引用传递 [英] Recursion function and pass by reference

查看:94
本文介绍了递归函数并通过引用传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好



我有这个程序使用评论版。但这只是我的方式。本教程希望通过引用传递来完成它。



所以我开始修改它,现在我有点卡住但几乎在那里。



你们可以看看它并告诉我这里缺少什么。



谢谢



Hello

I have this program working with the commented version. But that is just my way to do it. The tutorial wants it done by applying pass by reference.

SO I started modifying it and now I'm a little stuck but almost there.

Can you guys take a look at it and tell me what am I missing here.

Thanks

#include <iostream>
#include <cstdlib> //had to force it becasue my compiler (Code::Blocks) does not contain system.

using namespace std;
/*int n = 1, sum = 0;

int sumDigits(int n, int sum)
{
    //
	if (n == 0)
    {
        return sum;
    }
    else
    {
        // applying recursion and returning the value into the function
        sum = sum + n%10;
		n= n/10;
        return sumDigits(n, sum);
    }
}

int main(int argc, char* argv[])
{
	n = 1, sum = 0;

        cout << "Enter a non-negative integer: ";
        cin >> n;
        sum = sumDigits (n, sum);
        cout << "The sum of all digits "<< n << " is: " << sum << endl;

	system ("PAUSE");

        return 0;
}
*/

int sumDigits(int &);

int main()
{
	int n;
	sumDigits(n);
}

int sumDigits(int &n)
{
    cout << "Enter a non-negative integer: ";
    cin >> n;
        if (n == 1)
        {
            return 1;
        }
        else
        {
            return (n - 1) + n;
        }
    cout << "The sum of all digits "<< n << " is: " << n << endl;


	system ("PAUSE");

        return 0;
}

推荐答案

void sum_digits(int & n, int & sum)
{
  if ( n == 0 ) return;
  sum += n % 10;
  n /= 10;
  sum_digits(n, sum);
}

#include <iostream>
using namespace std;

int main()
{
  int n, sum=0;
  cout << "enter a non-negative number" << endl;
  cin >> n;
  if ( n < 0 ) return -1; // don't trust the user
  sum_digits(n,sum);
  cout << "sum is " << sum << endl;
}


当然,你可以使用传递参考,但首先看看我对这个问题的评论。



您提供的代码(未注释)有两个阻止程序问题:1) n 在第一次调用之前未初始化; 2)根本没有递归。



继续思考。即使我想给你一个完整的解决方案,我也不能,因为你的帖子不包含真正的规范,并且现有的代码都没有提供你想要实现的内容的明确描述。在形式上,这甚至不是一个可以接受的提问方法。即使您认为您的代码应被视为问题的正确规范,您也不能依赖它,因为不知道此代码是否正确。它确实表明问题是将以前的一些数字中的所有小数位相加,所以...



...如果上面的假设是正确的,那么代码的另一个问题(未注释掉)是:3)方法 sumDigits 包含输入;它不应该。



-SA
You can use passing by reference, of course, but first look at my comment to the question.

The code (not commented) you provided, has two blocker problem: 1) n is not initialized before the first call; 2) there is no recursion at all.

Keep thinking. Even if I wanted to give you a complete solution, I could not, because your post does not contain the real specifications, and none of the existing code does not provide unambiguous description of what do you want to achieve. Formally, this is not even an acceptable approach to asking questions. Even if you assumed that your code should be considered as a correct spec for the problem, you cannot rely on it, because it's not known if this code is correct or not. It does suggest that the problem is to sum all the decimal digits in some previously number, so…

…if the assumption above is correct, the other problem of the code (not commented out) is: 3) the method sumDigits contains input in it; it shouldn't.

—SA


这篇关于递归函数并通过引用传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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