Recursion 函数的递归幂函数,看看你能不能解出来

Recursion 函数的递归幂函数,看看你能不能解出来,recursion,Recursion,首先,这不是学校作业——只是我的好奇心,因为不知为什么我无法集中精力解决它。我总是想到这些愚蠢的事情,当我不能解决它们的时候,我会非常恼火 代码示例是用C语言编写的,但解决方案不必使用任何特定的编程语言 long powerofnum(short num, long powerof) { return powerofnum2(num, powerof, powerof); } long powerofnum2(short num, long powero

首先,这不是学校作业——只是我的好奇心,因为不知为什么我无法集中精力解决它。我总是想到这些愚蠢的事情,当我不能解决它们的时候,我会非常恼火

代码示例是用C语言编写的,但解决方案不必使用任何特定的编程语言

long powerofnum(short num, long powerof)
    {
        return powerofnum2(num, powerof, powerof);
    }

    long powerofnum2(short num, long powerof, long holder)
    {
        if (num == 1)
            return powerof;
        else
        {
            return powerof = powerofnum2(num - 1, holder * powerof, holder);
        }
    }
如你所见,我有两种方法。我调用powerofnum(value,powerofvalue),它然后调用下一个方法,第三个参数中的powerofvalue也作为占位符,这样它就可以通过递归记住原始的powerofvalue

我想要完成的是用一种方法来实现这一点。 我知道我可以在第一个方法中声明一个变量,使用powerof value来记住它,然后从0迭代到value of num。但由于这是一个理论问题,我希望递归地完成

在第一个方法中,我也可以像在第一个方法调用的第二个方法中一样,使用第三个参数whatever来存储值,但这看起来非常愚蠢。为什么要写两次似乎相同的参数

简单解释规则:

  • 无迭代
  • 仅限范围特定变量
  • 只有一种方法
无论如何,我希望有一个干净的解决方案

祝你好运:)

伪代码:

// power(x, y) == x^y, only works for positive integer y
function power(x, y) {
    return y <= 1 ? x : x * power(x, y - 1);
}
//幂(x,y)=x^y,仅适用于正整数y
功能功率(x,y){
返回y伪代码:

// power(x, y) == x^y, only works for positive integer y
function power(x, y) {
    return y <= 1 ? x : x * power(x, y - 1);
}
//幂(x,y)=x^y,仅适用于正整数y
功能功率(x,y){
返回y