从递归函数的输出不正确计算的一个号码的数字总和 [英] Incorrect output from recursive function to compute sum of digits of a number

查看:163
本文介绍了从递归函数的输出不正确计算的一个号码的数字总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写,将计算的一些使用递归数字之和功能,但输出不正确。这里的code:

I was trying to write a function that would compute the sum of the digits of a number using recursion, but the output is incorrect. Here's the code:

/*Write a function to calculate sum of digits of a  number using recursion*/
/*Author:Udit Gupta     Date:10/08/2011*/

#include<stdio.h>

int sum (int);

int main () {
    int n,s;

    printf ("Enter the number:");
    scanf ("%d",&n);

    s = sum (n);
    printf ("The sum of the digits of the number is %d",s);
}


int sum (int a) {
    int f;

    if (a == 0) {
         return f;
    }
    f = (a% 10) + sum (a/10);
}

下面是一些输出值:

 udit@udit-Dabba ~/Desktop/letusc/ch5/J $ ./a2.out
 Enter the number:123
 The sum of the digits of the number is 7

 udit@udit-Dabba ~/Desktop/letusc/ch5/J $ ./a2.out
 Enter the number:1234
 The sum of the digits of the number is 2919930

 udit@udit-Dabba ~/Desktop/letusc/ch5/J $ ./a2.out
 Enter the number:123456
 The sum of the digits of the number is 4620297

 udit@udit-Dabba ~/Desktop/letusc/ch5/J $ ./a2.out
 Enter the number:12345
 The sum of the digits of the number is 15  /*Only this one seems correct*/

有人可以帮助我弄清楚这是为什么不能正常工作?

Can someone help me figure out why this isn't working correctly?

推荐答案

让我们来看看这个递归函数的详细信息:

Let's look at this recursive function in more detail:

int sum (int a) {
    int f;

    if (a == 0)
        return f;

    f = (a% 10) + sum (a/10);
}

当你在正确的轨道,你有一般的想法是正确的,你的实际实施是一个有点马车。首先,让我们来看看这些行:

While you're on the right track and you have the right idea in general, your actual implementation is a bit buggy. For starters, let's look at these lines:

if (a == 0)
    return f;

您有正确的想法时 A 达到零终止递归,但你这样做的方式是有点过。特别是,你正在返回的整数 F的的价值,但是你从来没有初始化它。这意味着返回值是完全任意的。而不是写这个的,我想你大概意思写的东西更接近

You have the right idea to terminate the recursion when a reaches zero, but the way you're doing it is a bit off. In particular, you're returning the value of the integer f, but you've never initialized it. This means that the return value is completely arbitrary. Instead of writing this, I think that you probably meant to write something closer to

if (a == 0)
    return 0;

其中正确地说如果数是零,其数字的总和是零。

which correctly says "if the number is zero, the sum of its digits is zero."

同样,看看你的函数的最后一行:

Similarly, take a look at the last line of your function:

f = (a% 10) + sum (a/10);

再次你的直觉是点焊上:的若干数字的总和是由其第一数字的总和和其数字的其余部分的总和给出。但是,请注意,虽然你是正确的电脑的的数字的总和,你是不是正确的返回的数字的总和。其实,你不回在所有如果你执行这个code什么,所以从函数的返回值是不确定的,因此垃圾输出。为了解决这个问题,可以考虑改写code是这样的:

Again, your intuition is spot-on: the sum of the digits of a number are given by the sum of its first digit and the sum of the rest of its digits. However, notice that while you're correctly computing the sum of the digits, you aren't correctly returning the sum of the digits. In fact, you don't return anything at all if you execute this code, so the return value from the function is unspecified, hence the garbage output. To fix this, consider rewriting the code like this:

return (a % 10) + sum (a / 10);

这实际上是说,以手,而不是将其存储在将要立即清理,一旦函数返回一个局部变量回来,你刚才生成这里的价值。

This actually says to hand back the value that you just generated right here, instead of storing it in a local variable that will be immediately cleaned up as soon as the function returns.

我相信,你的原因codeD这个功能这种方式,你是在IM pression的 INT f的值; 是横跨函数调用进行。不幸的是,事实并非如此。当写一个递归函数,该函数的每个实例是完全相互独立的实例,在一个递归调用访问的本地变量不在其它递归调用访问。因此,即使每个递归调用都有自己的变量 INT˚F,这些变量都是完全相互独立的。该值未通过它们承载。如果你希望在整个递归函数沟通价值,做到这一点的最好办法是通过使用递归调用的返回值,或者(如果你一定要)通过递归指针传递给某个值了。

I believe that the reason you coded this function this way is that you're under the impression that the value of int f; is carried across the function calls. Unfortunately, it is not. When writing a recursive function, each instance of the function is completely independent of each other instance and local variables accessible in one recursive call are not accessible in other recursive calls. Consequently, even though each recursive call has its own variable int f, those variables are all completely independent of one another. The value isn't carried through them. If you want to communicate values across recursive functions, the best way to do it is by using the return value of the recursive calls, or (if you must) by passing a pointer to some value down through the recursion.

希望这有助于!

这篇关于从递归函数的输出不正确计算的一个号码的数字总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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