C 创建自己的幂函数的程序中出现运行时错误

C 创建自己的幂函数的程序中出现运行时错误,c,math,runtime-error,C,Math,Runtime Error,好的,我在读创建你自己的幂函数的程序() 我读了它的第一种方法,它使用这个函数计算功率: int power(int x, unsigned int y) { if( y == 0) return 1; else if (y%2 == 0) return power(x, y/2)*power(x, y/2); else return x*power(x, y/2)*power(x, y/2); } 我得到了这个程序的概

好的,我在读创建你自己的幂函数的程序()

我读了它的第一种方法,它使用这个函数计算功率:

int power(int x, unsigned int y)
{
    if( y == 0)
        return 1;
    else if (y%2 == 0)
        return power(x, y/2)*power(x, y/2);
    else
        return x*power(x, y/2)*power(x, y/2);

}
我得到了这个程序的概念,它给出了正确的结果

现在,这里写了
幂(x,y/2)*幂(x,y/2)
,所以我们只是计算
幂(x,y/2)的平方。因此,如果我的
power()
函数正确,那么我可以将其更改为
power(power(x,y/2),2)
。也就是说,我们只是计算幂的平方(x,y/2)

因此,当我将我的程序更改为:

int power(int x, unsigned int y)
{
    if( y == 0)
        return 1;
    else if (y%2 == 0)
        return power(power(x, y/2),2);   // Square of power(x,y/2)
    else
        return x*power(power(x, y/2),2);   // x*Square of power(x,y/2)

}
int main()
{
    int x = 2;
    unsigned int y = 3;

    printf("%d\n", power(x, y));
    return 0;
}
上述程序给出运行时错误


我无法找出运行时错误的原因是什么。有人能帮我吗?

您正在从函数本身内部调用函数
power
,将2作为第二个参数传递

这本质上是一个无限递归,最终导致堆栈溢出


如果输入参数是非负整数,则可以按如下方式实现:

递归:

unsigned long long power(unsigned long long x,unsigned int y)
{
    if (y == 0)
        return 1;
    return power(x,y/2)*power(x,y-y/2);
}
unsigned long long power(unsigned long long x,unsigned int y)
{
    unsigned long long res = 1;
    while (y--)
        res *= x;
    return res;
}
unsigned long long power(unsigned long long x,unsigned int y)
{
    unsigned long long res = 1;
    while (y > 0)
    {
        if (y & 1)
            res *= x;
        y >>= 1;
        x *= x;
    }
    return res;
}
迭代:

unsigned long long power(unsigned long long x,unsigned int y)
{
    if (y == 0)
        return 1;
    return power(x,y/2)*power(x,y-y/2);
}
unsigned long long power(unsigned long long x,unsigned int y)
{
    unsigned long long res = 1;
    while (y--)
        res *= x;
    return res;
}
unsigned long long power(unsigned long long x,unsigned int y)
{
    unsigned long long res = 1;
    while (y > 0)
    {
        if (y & 1)
            res *= x;
        y >>= 1;
        x *= x;
    }
    return res;
}
高效:

unsigned long long power(unsigned long long x,unsigned int y)
{
    if (y == 0)
        return 1;
    return power(x,y/2)*power(x,y-y/2);
}
unsigned long long power(unsigned long long x,unsigned int y)
{
    unsigned long long res = 1;
    while (y--)
        res *= x;
    return res;
}
unsigned long long power(unsigned long long x,unsigned int y)
{
    unsigned long long res = 1;
    while (y > 0)
    {
        if (y & 1)
            res *= x;
        y >>= 1;
        x *= x;
    }
    return res;
}

您正在从函数本身内部调用函数
power
,将2作为第二个参数传递

这本质上是一个无限递归,最终导致堆栈溢出


如果输入参数是非负整数,则可以按如下方式实现:

递归:

unsigned long long power(unsigned long long x,unsigned int y)
{
    if (y == 0)
        return 1;
    return power(x,y/2)*power(x,y-y/2);
}
unsigned long long power(unsigned long long x,unsigned int y)
{
    unsigned long long res = 1;
    while (y--)
        res *= x;
    return res;
}
unsigned long long power(unsigned long long x,unsigned int y)
{
    unsigned long long res = 1;
    while (y > 0)
    {
        if (y & 1)
            res *= x;
        y >>= 1;
        x *= x;
    }
    return res;
}
迭代:

unsigned long long power(unsigned long long x,unsigned int y)
{
    if (y == 0)
        return 1;
    return power(x,y/2)*power(x,y-y/2);
}
unsigned long long power(unsigned long long x,unsigned int y)
{
    unsigned long long res = 1;
    while (y--)
        res *= x;
    return res;
}
unsigned long long power(unsigned long long x,unsigned int y)
{
    unsigned long long res = 1;
    while (y > 0)
    {
        if (y & 1)
            res *= x;
        y >>= 1;
        x *= x;
    }
    return res;
}
高效:

unsigned long long power(unsigned long long x,unsigned int y)
{
    if (y == 0)
        return 1;
    return power(x,y/2)*power(x,y-y/2);
}
unsigned long long power(unsigned long long x,unsigned int y)
{
    unsigned long long res = 1;
    while (y--)
        res *= x;
    return res;
}
unsigned long long power(unsigned long long x,unsigned int y)
{
    unsigned long long res = 1;
    while (y > 0)
    {
        if (y & 1)
            res *= x;
        y >>= 1;
        x *= x;
    }
    return res;
}

您将向这个函数传递什么输入?当然,提供一个完整的程序是很容易的,这样我们就不必猜测了。@DavidHeffernan UpdatedStack overflow,当您从内部调用函数
power
,将第二个参数传递为
2
。您将向该函数传递什么输入?当然,提供一个完整的程序是很容易的,这样我们就不必猜测了。@DavidHeffernan UpdatedStack overflow,因为您从内部调用函数
power
,将第二个参数传递为
2
。对于三种方法,+1对于三种方法,+1