这是功率实现的函数,我也得到了逻辑和代码,但在偶数情况下,输出中没有填充数字和功率

问题描述

这是一个功率实现函数我也得到了逻辑和代码,但在偶数情况下,数字和功率没有填入输出输出显示正确答案??

公共类Implementpowerfunc {

static int powerfunc(int x,int y) {
    if(y==0) {
        return 1;
    }
    int output = 1;
    while(y>0) {
        if(y%2==0) {        // even case(output ??)
            x=x*x;
            y=y/2;
        }
        else {               // odd case 
            output = x*output;
            y--;
        }
    }
    return output;
}

解决方法

最好展示一个 2 的 5 次方的例子。

x y 输出
2 5 1
2 4 2
4 2 2
16 1 2
16 0 32