小编Raj*_*tel的帖子

候选函数不可行:期望第三个参数的l值

使用递归函数myPowerFunction(int p,int n,int和currentCallNumber)计算P的n次幂(p和n都是正整数).currentCallNumber是一个引用参数,用于存储到目前为止所进行的函数调用次数.myPowerFunction返回p的第n个幂.

int myPowerFunction(int p, int n, int &z)
{
    z++;
    if(n==1)return p;
    else if(n==0)return 1;
    else if(n%2==0)return myPowerFunction(p,n/2,z)*myPowerFunction(p,n/2,z);
    else return myPowerFunction(p,n/2,z)*myPowerFunction(p,n/2,z)*p;
}

int main()
{
    cout << myPowerFunction(3,4,1);
}
Run Code Online (Sandbox Code Playgroud)

c++ pass-by-reference

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

c++ ×1

pass-by-reference ×1