Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用递归计算幂的幂?_Java_Recursion_Exponent - Fatal编程技术网

Java 如何使用递归计算幂的幂?

Java 如何使用递归计算幂的幂?,java,recursion,exponent,Java,Recursion,Exponent,我最近一直在研究递归,它非常混乱。我正在尝试编写一个java程序,它使用递归来生成一个基数到一个幂的结果,这个幂的高度也就是基数有多少个指数。例如,底面为2,幂为3,高度为3,将产生以下结果: ((2^3)^3)^3) 以下是我的(非常有缺陷的)代码: publicstaticvoidmain(字符串[]args){ System.out.println(高度指数(2,2,3)); } 公共静态整数指数WithHeight(整数基、整数幂、整数高){ 如果(功率

我最近一直在研究递归,它非常混乱。我正在尝试编写一个java程序,它使用递归来生成一个基数到一个幂的结果,这个幂的高度也就是基数有多少个指数。例如,底面为2,幂为3,高度为3,将产生以下结果:

((2^3)^3)^3)

以下是我的(非常有缺陷的)代码:

publicstaticvoidmain(字符串[]args){
System.out.println(高度指数(2,2,3));
}
公共静态整数指数WithHeight(整数基、整数幂、整数高){
如果(功率<0 | |高度<0){
抛出新的IllegalArgumentException(“检查输入值”);
}
如果(功率==0){
返回1;
}否则{
返回值(基数*高度*高度指数(基数,幂-1,高度-1));
}
}

我真的不知道如何去获得我想要的输出;正如我所说的,这对我来说真的很困惑和陌生。有人能解释一下怎么做吗?

这是一个双重递归,包括高度和功率

请注意,如果将这些值缓存在某个地方是有意义的,因为exponentWithHeight(base,power,0)将被重新计算多次

此代码的结果为256:

public static void main(String[] args) {
    System.out.println(exponentWithHeight(2, 2, 3));
}

public static int exponentWithHeight(int base, int power, int height) {
    if (power < 0 || height < 0) {
        throw new IllegalArgumentException("Invalid Input; Check power and/or height value.");
    }
    if (base == 0) {
        return 0;
    }
    if (height == 0) {
        return calcPower(base, power);
    } else {
        return exponentWithHeight(calcPower(base, power), power, height - 1);
    }
}

public static int calcPower(int base, int power) {
    if (power == 0) {
        return 1;
    }
    return base * power(base, power - 1);
}
publicstaticvoidmain(字符串[]args){
System.out.println(高度指数(2,2,3));
}
公共静态整数指数WithHeight(整数基、整数幂、整数高){
如果(功率<0 | |高度<0){
抛出新的IllegalArgumentException(“无效输入;检查电源和/或高度值”);
}
如果(基==0){
返回0;
}
如果(高度==0){
返回计算功率(基准、功率);
}否则{
返回高度指数(calcPower(base,power),power,height-1);
}
}
公共静态int-calcPower(int-base,int-power){
如果(功率==0){
返回1;
}
返回基*功率(基,功率-1);
}

我们是否忘记了可以将一系列指数相乘以简化方程

以下几点应该行得通

public static int exponentWithHeight(int base, int power, int height) {
    if (power < 0 || height < 0) {
        throw new IllegalArgumentException("Invalid Input; Check power and/or height value.");
    }
    if (power == 0 || height == 0) {
        return 1;
    } else {
        return (int) Math.pow(base, Math.pow(power, height));
    }
}
public static int-exponentWithHeight(int-base、int-power、int-height){
如果(功率<0 | |高度<0){
抛出新的IllegalArgumentException(“无效输入;检查电源和/或高度值”);
}
如果(功率=0 | |高度=0){
返回1;
}否则{
return(int)Math.pow(base,Math.pow(power,height));
}
}

这里是另一个简单明了的例子,使用for循环计算幂,然后使用递归计算高度

public static long exponentWithHeight(long base, long power, long height) {
            long calculatedPower;
            if (power < 0 || height < 0) {
                throw new IllegalArgumentException("Invalid Input; Check power and/or height value.");
            }
            if (power == 0) {
                return 1;
            }
            if (height > 0) {
                calculatedPower = base;
                for (int i = (int) power; i > 1; i--) {
                        calculatedPower *= base;
                }
                return exponentWithHeight(calculatedPower, power, --height);
            }

            return base;

    }
带高度的公共静态长指数(长底座、长功率、长高度){
长计算功率;
如果(功率<0 | |高度<0){
抛出新的IllegalArgumentException(“无效输入;检查电源和/或高度值”);
}
如果(功率==0){
返回1;
}
如果(高度>0){
计算功率=基数;
对于(int i=(int)幂;i>1;i--){
计算功率*=基数;
}
返回高度指数(计算功率,功率,--高度);
}
返回基地;
}

您的预期输出是什么,您得到了什么?调试时您发现了什么?因此,当使用调用方法exponentWithHeight(2,2,3)时,我的预期输出将是256。相反,我得到24分。当我调试它时,一切都运行得很好。“一切都运行得很好”你是什么意思?调试时得到256?或者你没有收到任何错误?(后者是没有意义的,如果你没有得到正确的结果,你显然有一个错误)记住,你可以通过将一系列指数相乘来简化它们。所以
((2^3)^3)^3)=2^(3*3*3)=2^(3^3)
-有效地使你的公式
f(n,exp,height)=n^(exp^ height)
。也许可以利用这一点简化您提出的任何解决方案。这非常有帮助,谢谢!没有这样想。除了
Math.pow
返回一个
double
,而不是
int
,我认为OP的练习重点不是使用
Math.pow
@和yturner OP从未指定他们不想使用递归。标题指定OP确实想(/need)使用递归:“如何使用递归计算幂的幂?”@AndyTurner,如果这是一个X-Y问题怎么办?实际上,OP的解决方案很差。这段代码似乎没有正确计算值;当我输入(3,2,2)时,我得到的答案不是81,而是729。当你乘法时,到底发生了什么[exponentWithHeight(基底,功率,0)*exponentWithHeight(基底,功率,高度-1)]?编辑它。获取第一个基数和幂的值。然后从幂中减去一。谢谢!这很容易阅读,并且像一个符咒一样工作。不客气。但是你应该记住,此代码只适用于小幂和高度数字,因为如果你对幂或高度(如4或以上)使用一些大的数字,计算出的数字r将超过
long
数据类型最大值的容量,并返回0。
public static long exponentWithHeight(long base, long power, long height) {
            long calculatedPower;
            if (power < 0 || height < 0) {
                throw new IllegalArgumentException("Invalid Input; Check power and/or height value.");
            }
            if (power == 0) {
                return 1;
            }
            if (height > 0) {
                calculatedPower = base;
                for (int i = (int) power; i > 1; i--) {
                        calculatedPower *= base;
                }
                return exponentWithHeight(calculatedPower, power, --height);
            }

            return base;

    }