java - 这是电源实现的功能,我也得到了逻辑和代码,但在偶数情况下,输出中没有填充数字和电源
问题描述
这是电源实现的功能我也得到了逻辑和代码,但在偶数情况下,输出中没有填充数字和电源,但输出显示正确答案?
公共类实施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;
}
解决方案
最好以 2 的 5 次方为例。
X | 是的 | 输出 |
---|---|---|
2 | 5 | 1 |
2 | 4 | 2 |
4 | 2 | 2 |
16 | 1 | 2 |
16 | 0 | 32 |
推荐阅读
- vb.net - 如何压缩多个文件并在压缩时拆分zip
- android - Android Release + Firebase + Google Tag Manager + Google Analytics 的 ecommerce_purchase 问题
- mysql - 导入sql文件时出现UTF-8编码问题
- reactjs - 在 React 可重用组件中生成组件数组的更好方法
- css - 下拉箭头重力形式
- angular - rxjs:我怎样才能得知取消订阅 observable
- command-line-interface - Picocli:使用可选值指定选项的最佳方法,当未指定值时打印当前值
- google-drive-api - 通过 python api 将 zip 文件上传到谷歌团队驱动器
- amazon-web-services - 公开存储桶后,Amazon S3 存储桶中的文件被拒绝
- php - WooCommerce 向会员免费送货导致错误