Udi*_*pta 2 c algorithm recursion
我试图编写一个函数来计算使用递归的数字的总和,但输出是不正确的.这是代码:
/*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);
}
Run Code Online (Sandbox Code Playgroud)
以下是一些输出值:
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*/
Run Code Online (Sandbox Code Playgroud)
有人可以帮我弄清楚为什么这不能正常工作?
让我们更详细地看一下这个递归函数:
int sum (int a) {
int f;
if (a == 0)
return f;
f = (a% 10) + sum (a/10);
}
Run Code Online (Sandbox Code Playgroud)
当你走在正确的轨道上并且你有正确的想法时,你的实际实现有点儿错误.首先,让我们来看看这些内容:
if (a == 0)
return f;
Run Code Online (Sandbox Code Playgroud)
你有正确的想法,当a到达零时终止递归,但你做的方式有点偏.特别是,您将返回整数的值f,但您从未初始化它.这意味着返回值完全是任意的.而不是写这个,我认为你可能想写一些更接近的东西
if (a == 0)
return 0;
Run Code Online (Sandbox Code Playgroud)
正确地说"如果数字为零,则其数字之和为零."
同样,看一下函数的最后一行:
f = (a% 10) + sum (a/10);
Run Code Online (Sandbox Code Playgroud)
同样,你的直觉是现场的:数字的总和由第一个数字的总和和其余数字的总和给出.但是,请注意,虽然您正确计算了数字的总和,但您没有正确返回数字的总和.实际上,如果执行此代码,则根本不返回任何内容,因此函数的返回值未指定,因此是垃圾输出.要解决此问题,请考虑重写代码,如下所示:
return (a % 10) + sum (a / 10);
Run Code Online (Sandbox Code Playgroud)
这实际上是说要在这里交回刚才生成的值,而不是将其存储在一个局部变量中,该变量将在函数返回后立即清除.
我相信你用这种方式编写这个函数的原因是你的印象int f;是函数调用的值.不幸的是,事实并非如此.在编写递归函数时,函数的每个实例都完全独立于每个其他实例,并且在一个递归调用中可访问的局部变量在其他递归调用中是不可访问的.因此,即使每个递归调用都有自己的变量int f,这些变量也完全相互独立.该值不是通过它们传递的.如果要在递归函数之间传递值,最好的方法是使用递归调用的返回值,或者(如果必须)通过将指针传递给递归的某个值.
希望这可以帮助!