package sort;
public class Two_n {
public static void main(String[] args) {
System.out.println(power2(10));
}
public static int power(Integer n) {
int flag=1;
while(flag<=n) {
if(flag<n) {
flag*=2;
}
else {
return 1;
}
}
return 0;
}
public static int power2(Integer n) {
if(n<0) {
return 0;
}
if(n==0) {
return 1;
}else {
return 2*power2(n-1);
}
}
}
2的n次幂java实现方法
最新推荐文章于 2023-03-09 17:49:07 发布
这篇博客探讨了两种计算幂的方法:`power`和`power2`。`power`方法使用循环结构,当标志`flag`小于等于输入的`n`时不断翻倍,直到达到或超过`n`。而`power2`方法采用递归方式,对于非负整数`n`,返回2的`n-1`次方的两倍。博客深入解析了这两种计算幂的不同实现策略。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Stable-Diffusion-3.5
图片生成
Stable-Diffusion
Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率
1968

被折叠的 条评论
为什么被折叠?



