使用递归乘以幂
我正在寻找一种方法来编写一个仅使用递归循环将整数乘以指数的程序。我对递归的理解非常有限,但已经能够编写一些代码来给出阶乘:
int fac2(int n)
{
if (n == 1){
return 1;
} else {
return n*fac2(n-1);
}
}
我已经有办法找到幂,但它使用 for
循环:
int my_power(int x, int e)
{
int i, total;
total = 1;
for (i = 1; i <= e; i++){
total *= x;
}
return total;
}
我怎样才能替换它for循环使用递归?
I am looking for a way to code a program which will multiply an integer to an exponent using only a recursion loop. I have a very limited understanding of recursion, but have been able to code something to give a factorial:
int fac2(int n)
{
if (n == 1){
return 1;
} else {
return n*fac2(n-1);
}
}
I have a way to find a power already, but it uses a for
loop:
int my_power(int x, int e)
{
int i, total;
total = 1;
for (i = 1; i <= e; i++){
total *= x;
}
return total;
}
How can I replace this for loop using recursion?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
请记住,递归函数会调用自身,直到实现某些基本情况。您的基本情况是什么?计算一个数的幂就像说你要乘以某个数 x 次。提示是调用递归函数,将幂减一,直到达到所需的基本情况。
Remember that a recursive function calls itself until some base case is achieved. What is your base case here? Raising a number to a power is liking saying that you are going to multiply some number x amount of times. The hint is to call the recursive function, reducing the power by one until you reach your desired base case.
使用戴夫·安德森(Dave Anderson)作为基础,但也要考虑以下特殊情况:
同样,没有代码,因此您可以尝试自己弄清楚:-)
更新:确保创建许多测试用例,并且确保每个人都按照您认为应该的方式工作。一旦测试通过,您就会知道递归幂函数工作正常。
示例:有时间自己弄清楚后,我想我会提出一个带有测试的解决方案:
输出:
Use Dave Anderson's as a basis, but also consider the special cases of where:
Again, no code so you can try to figure it out yourself :-)
Update: Make sure you create a number of test cases, and you make sure everyone works as you think it should. Once the tests pass, you'll know that your recursive power function is working correctly.
Example: Having had time to figure it out yourself, I though I'd present a solution, with tests:
Output:
做你想做的事情的正确方法是注意:
其中“⊦n/2⫞”表示 n/2 的整数部分(当 n 是整数变量时,这就是 n/2 在 C 中给出的值)。
与阶乘相反,阶乘为
您应该能够编写一个基于阶乘的递归程序来求幂。
The right way of doing what you want is by noticing that:
where "⊦n/2⫞" means the integer part of n/2 (which is what n/2 gives you in C when n is an integer variable).
Contrasting with the factorial, which reads
you should be able to write a recursive program for exponentiation basing yourself on the factorial.
有一种思考这个问题的方法需要我们走出C-land,但它的核心如此简单且强大,非常值得考虑。
递归和迭代并不像乍看起来那么不同。在一定水平 ,实际上很难区分它们。我们不会完全去那里,但考虑一下 for“循环”的递归实现(没有特定的语言):(
a
,顺便说一句,被称为“累加器”,因为它累积每个“循环”的值)您可能会感到陌生的一件事是,上面将函数视为任何其他数据类型,这就是所谓的“一流函数"。这通常不是您在 C 中执行的操作(您可以通过传递函数指针以有限的方式执行此操作),但它是一个非常强大的概念,非常值得学习。
使用上面的
for
定义,我们将阶乘函数编写为:此推杆将每个
i
乘以累加器。另一个强大的概念(遗憾的是,C 根本不支持)是匿名函数,它只是创建的没有名称的函数。我将使用语法
e ->; ...
对于匿名函数,其中...
是某个表达式(例如x -> x+1
),而(a, b)
对于一对变量。在 C 中,匿名函数可能看起来毫无用处,但如果您可以像传递数字一样轻松地传递函数,则可以将fac
简化为一行:pow
可以写成:展开
for
,你就得到了pow
的递归定义。尽管这为您提供了递归函数,但还有另一种实现(实际上是 Alexandre 的)更省时。
There's a way of thinking about this that requires us stepping outside of C-land, but it's so simple at it's heart and powerful it's well worth considering.
Recursion and iteration aren't so different as they first seem. At a certain level, it's actually hard to tell them apart. We won't quite go there, but consider this recursive implementation of a for "loop" (in no particular language):
(The
a
, by the way, is called an "accumulator", because it accumulates the value of each "loop")The one thing that might be new to you is that the above treats functions like any other data type, which is something called "first-class functions". This isn't typically something you do in C (you can do it in a limited fashion by passing function pointers), but it's so powerful a concept that it's well worth learning.
Using the above definition of
for
, we write your factorial function as:This putts along, multiplying each
i
by the accumulator.Another powerful concept (that, sadly, C doesn't support at all) is anonymous functions, which are simply functions created without names. I'm going to use the syntax
e -> ...
for anonymous functions, where...
is some expression (e.g.x -> x+1
), and(a,b)
for a pair of variables. In C, anonymous functions might seem useless, but if you can pass around functions as easily as you can pass around numbers, you can simplifyfac
to a single line:pow
can be written as:Expand
for
in that, and you've got a recursive definition forpow
.Though this gives you a recursive function, there is another implementation (Alexandre's, in fact) that's more time efficient.
据我所知,此函数对于任何可能的情况(不太可能将“asdfj”设置为 x 或 e)都有效。
This function will, to the best of my knowledge, for any likely case (someone setting "asdfj" as x or e is not likely) work.
这是给您的伪代码:
哦,返回值应该是
double
。这是实际的代码:
Here's a pseudo-code for you:
And oh, the return value should be
double
.Here's the actual code: