首页 > 解决方案 > 这是电源实现的功能,我也得到了逻辑和代码,但在偶数情况下,输出中没有填充数字和电源

问题描述

这是电源实现的功能我也得到了逻辑和代码,但在偶数情况下,输出中没有填充数字和电源,但输出显示正确答案?

公共类实施powerfunc {

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;
}

标签: javaalgorithmdata-structuresbinary-search

解决方案


最好以 2 的 5 次方为例。

X 是的 输出
2 5 1
2 4 2
4 2 2
16 1 2
16 0 32

推荐阅读