可以帮助我吗? 是Tail Rercursion吗? i只想得到关于尾递归的答案和一些例子 尾部递归,这样我就可以区分尾部和非b / b 尾部累积。int harmonic(int n) {if (n=1) {return 1;}else {return harmonic(n-1)+1/n;}}can any help me ??Is it Tail Rercursion??i just want the answer about tail recursion and and some examplesof tail recursion so that i can differentiate between tail and non-tail recrsion.推荐答案您好, 我相信,根据定义,它不是。在第二种情况下, 函数不会返回_exactly_递归 调用返回的值(有一个必须添加和除法操作在返回值之前执行。 有关定义,请参阅: http://www.google.com/search?hl=en&q...tail+recursion 问候。Hello,I believe that, by definition, it is not. In the second case thefunction does not return _exactly_ the value returned by a recursivecall (there is an addition and a division operation that have to beperformed before returning the value).For definitions, see: http://www.google.com/search?hl=en&q...tail+recursionRegards. On 2008-11-03 03:55:06 -0500,Muzammil< mu ** ***********@gmail.comsaid:On 2008-11-03 03:55:06 -0500, Muzammil <mu*************@gmail.comsaid: int harmonic(int n){ if(n = 1){ 返回1; } else { 返回谐波(n-1)+ 1 / n; } } 可以帮助我吗? 是吗Tail Rercursion ?? i只想得到关于尾递归的答案和一些例子 的尾递归让我可以区分尾巴和非尾巴之间的转换。int harmonic(int n) {if (n=1) {return 1;}else {return harmonic(n-1)+1/n;}}can any help me ??Is it Tail Rercursion??i just want the answer about tail recursion and and some examplesof tail recursion so that i can differentiate between tail and non-tail recrsion. 如果稍微更改最后一行,答案会更清晰: 返回1 / n +谐波(n -1); 由于函数的尾部是对函数的调用,这是尾部 递归。它可以被一个简单的循环取代,这是许多编译器所做的优化。 - Pete Roundhouse Consulting,Ltd。( www.versatilecoding.com )作者 标准C ++库扩展:教程和参考 ( www.petebecker.com/tr1book )If you change the last line slightly the answer becomes a bit clearer:return 1/n + harmonic(n-1);Since the tail of the function is a call to the function, this is tailrecursion. It could be replaced by a simple loop, which is anoptimization that many compilers do.--PeteRoundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "TheStandard C++ Library Extensions: a Tutorial and Reference(www.petebecker.com/tr1book) On 2008-11-03 06:18:44 -0500, al**********@gmail.com 说:On 2008-11-03 06:18:44 -0500, al**********@gmail.com said: 您好, 我相信,根据定义,它不是。在第二种情况下, 函数不会返回_exactly_递归 调用返回的值(有一个必须添加和除法操作在返回值之前执行。 有关定义,请参阅: http://www.google.com/search?hl=en&q...tail+recursion 问候。Hello,I believe that, by definition, it is not. In the second case thefunction does not return _exactly_ the value returned by a recursivecall (there is an addition and a division operation that have to beperformed before returning the value).For definitions, see: http://www.google.com/search?hl=en&q...tail+recursionRegards. 大多数编译器应该能够通过识别尾递归来优化原始代码到 循环。这不是返回 的问题,而是递归调用返回的值(这将是一个非常小的函数集,无用),但是是否可以将递归调用 更改为循环。一个例子不能是Ackerman的 函数: int ackerman(unsigned i,unsigned j) { if(i == 0) 返回j + 1; 否则if(j == 0) 返回ackerman(i - 1,1); else 返回ackerman(i - 1,ackerman(i,j - 1)); } 结果取决于多个递归调用,因此函数不能将转换为一个简单的循环。它也快速增长;观看 溢出。 - Pete Roundhouse Consulting,Ltd。( www.versatilecoding.com ) 标准的作者C ++库扩展:教程和参考 ( www.petebecker.com / tr1book )Most compilers ought to be able to optimize the original code into aloop by recognizing the tail recursion. It''s not a matter of returningexactly the value returned by the recursive call (that would be a verysmall set of functions, all useless), but of whether the recursive callcan be changed into a loop. An example that can''t is Ackerman''sfunction:int ackerman(unsigned i, unsigned j){if (i == 0)return j + 1;else if (j == 0)return ackerman(i - 1, 1);elsereturn ackerman(i - 1, ackerman(i, j - 1));}The result depends on multiple recursive calls, so the function can''tbe transformed into a simple loop. It also grows very quickly; watchout for overflows.--PeteRoundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "TheStandard C++ Library Extensions: a Tutorial and Reference(www.petebecker.com/tr1book) 这篇关于是尾递归吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-21 20:36