递归函数 [英] Recursive Functions
问题描述
我正在尝试写一个Power(x,n)的递归版本,它的工作原理是
将n分成两半(n = n / 2的一半),平方
Power(x,n / 2),如果n为奇数,则再次乘以x,并找到一个
合适的基本情况来停止递归。有人可以给我一个
的例子吗?
谢谢!
< blockquote> On Mon,27 Oct 2003 09:54:05 -0800,dmattis写道:
我正在尝试编写一个递归版本的Power(x,n),它的工作原理是<将n分成两半(其中n = n / 2的一半),平方
Power(x,n / 2),如果n为奇数则再次乘以x,并找到
合适的基础案例来阻止递归。有人可以给我一个
这样的例子吗?
这个问题不在这里。我建议你尝试在comp.programming中发布
,但我实际上无法弄明白你想要什么。你已经完全描述了算法,
你还想要什么?
-Sheldon
p.s.我希望你想要的东西不是为了某人简单地为你做功课。
在2003-10-27,dmattis< dm ***** @ yahoo.com>写道:我正在尝试写一个递归版本的Power(x,n),它的工作原理是将n分成两半(n = n / 2的一半),平方<如果n是奇数,则功率(x,n / 2)再次乘以x,并找到一个合适的基本情况来停止递归。有人可以给我一个这样的例子吗?
这不是C问题......但C答案是使用pow()(除非你
是故意避免浮点数,在这种情况下表格查询
是有序的。)
< DYOHW>将您的单词问题重写为重复关系。使用
归纳来证明这种重复正确计算x到第n个
的功率。在这一点上应该很明显你的功能应该是什么样的b $ b看起来像你的基本情况。 < / DYOHW>
- James
dmattis写道:
我正在尝试写一个递归版本的Power(x,n),通过将n分解成两半(n = n / 2的一半),平方
Power(x,n / 2)如果n是奇数,则再次乘以x,并找到一个合适的基本情况来停止递归。有人可以给我一个这样的例子吗?
这似乎是一个相当清楚的程序描述,
假设`n ''是一个非负整数。 (如果'n''可以是
否定问题就更难了;如果'n''可以是非整数
问题要困难得多。)问题是什么你有
吗?也许如果你向我们展示你到目前为止写的东西,
有人可以提供帮助。
-
Er*********@sun.com
I am trying to write a recursive version of Power(x,n) that works by
breaking n down into halves(where half of n=n/2), squaring
Power(x,n/2), and multiplying by x again if n was odd, and to find a
suitable base case to stop the recursion. Can someone give me an
example of this?
Thanks!
On Mon, 27 Oct 2003 09:54:05 -0800, dmattis wrote:
I am trying to write a recursive version of Power(x,n) that works by
breaking n down into halves(where half of n=n/2), squaring
Power(x,n/2), and multiplying by x again if n was odd, and to find a
suitable base case to stop the recursion. Can someone give me an
example of this?
This question is off topic here. I would suggest you try
posting in comp.programming, but I can''t actually figure out
what you want. You''ve described the algorithm completely,
what more do you want?
-Sheldon
p.s. I''m hoping that what you want isn''t for someone to simply
do your homework for you.
On 2003-10-27, dmattis <dm*****@yahoo.com> wrote:I am trying to write a recursive version of Power(x,n) that works by
breaking n down into halves(where half of n=n/2), squaring
Power(x,n/2), and multiplying by x again if n was odd, and to find a
suitable base case to stop the recursion. Can someone give me an
example of this?
This is not a C question... but the C answer is use pow() (unless you
are purposefully avoiding floating point, in which case a table lookup
is in order).
<DYOHW> Rewrite your word problem into a recurrence relationship. Use
induction to prove this recurrence correctly calculates x to the nth
power. It should be obvious at that point what your function should
look like and what your base case is. </DYOHW>
-- James
dmattis wrote:
I am trying to write a recursive version of Power(x,n) that works by
breaking n down into halves(where half of n=n/2), squaring
Power(x,n/2), and multiplying by x again if n was odd, and to find a
suitable base case to stop the recursion. Can someone give me an
example of this?
This seems a fairly clear description of the procedure,
assuming `n'' is a non-negative integer. (If `n'' can be
negative the problem is harder; if `n'' can be a non-integer
problem is much harder.) What problem are you having with
it? Perhaps if you''d show us what you''ve written thus far,
someone will be able to help.
--
Er*********@sun.com
这篇关于递归函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!