Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.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 递归方法(将2的幂和作为整数返回到2的第x次幂),而不使用Math.pow_Java_Recursion - Fatal编程技术网

Java 递归方法(将2的幂和作为整数返回到2的第x次幂),而不使用Math.pow

Java 递归方法(将2的幂和作为整数返回到2的第x次幂),而不使用Math.pow,java,recursion,Java,Recursion,我目前正在创建一个递归代码,该代码包含5个方法等。我的递归方法有问题,它将返回并在main()中打印2的幂和到2的第x次幂。X是命令行参数上的整数 我能做math.pow过程,但是如果不使用math.pow,我怎么做呢 我的代码(问题是powerCount方法) 以下是powerCount方法: public static int powerCount(int initialNum, int finalNum) { //base case if(initialNum

我目前正在创建一个递归代码,该代码包含5个方法等。我的递归方法有问题,它将返回并在main()中打印2的幂和到2的第x次幂。X是命令行参数上的整数

我能做math.pow过程,但是如果不使用math.pow,我怎么做呢

我的代码(问题是powerCount方法)

以下是powerCount方法:

public static int powerCount(int initialNum, int finalNum)
   {
      //base case
      if(initialNum == finalNum){
         return initialNum;
      }
      //recursive case
      else{
         return initialNum + powerCount(initialNum *2, finalNum);
      }//end of else
   }//end of method5
}//end of class
这里还有main中调用/返回并打印它的方法

// method call to 5th method    
result2 = powerCount(1,(int)Math.pow(2,num));
// display output of 5th method to screen    
System.out.println(result2);

您最大的问题是代码太多,所以我不打算调试它

递归impl非常简单,如下所示:

int pow(int x, int p) {
    return p == 0 ? 1 : x * pow(x, p - 1);
}

如果你想打印东西,不要用这种方法。我会在代码之外的一个主循环中执行它,该循环将p从1迭代到n(忘记“效率”——这将在几微秒内执行)。

一个可用的函数应该是:

/**
 * Calculates n<sup>exp</sup>.
 * @param n the base number.
 * @param exp the exponent.
 * @return the power of n to the exp.
 */
public static int power(int n, int exp) {
    // What is the easiest case (terminating the recursion?
    // - Case exp == 0 return 1
    ...
    // - Case other exp values
    // What do you know about power formulas to reduce exp?
    ... recursion with smaller exp
}
/**
*计算nexp。
*@param n是基数。
*@param exp表示指数。
*@将n的力量返回给exp。
*/
公共静态整数功率(整数n,整数exp){
//最简单的情况是什么(终止递归)?
//-Case exp==0返回1
...
//-其他exp值的情况
//关于减少exp的幂公式,你知道些什么?
…具有较小exp的递归
}
当然
n^exp+1
=
n*n^exp
可以将exp减少1。但也许你可以找到更好的减少

在您的例子中,块可能是非数学长名称,有时会模糊一些东西。使用
x^n
作为名称可能更有用

同样,递归从
f(x,y)
开始,在下一步中使用
f(x',y')
,这是一种简化。因此,对于最后一步
y'
是一个很小的例子,
x'
可能已经是结果了


在我们的例子中,
power(x,n)
将有最后一个递归调用
power(x\u-up\n,1)
x\u-up\n*power(rumpelstielchen,0)

(问题是powerCount方法)。
我们只需发布相关代码即可。对不起,请编辑
Math.pow(x,2)
相当于
x*x
而不是
x*2
有没有办法不做数学。pow?
1
/**
 * Calculates n<sup>exp</sup>.
 * @param n the base number.
 * @param exp the exponent.
 * @return the power of n to the exp.
 */
public static int power(int n, int exp) {
    // What is the easiest case (terminating the recursion?
    // - Case exp == 0 return 1
    ...
    // - Case other exp values
    // What do you know about power formulas to reduce exp?
    ... recursion with smaller exp
}