使用递归来创建幂方法

时间:2015-06-29 01:40:47

标签: java recursion this pseudocode

我是编程新手,Java是我的第一个真正的语言。在课堂上的这一点上,我们正在接触递归,并且被要求制作一个适合某些参数的方法:

方法是这样的:public void power(int p)我们无法改变方法的形式参数。我在其他地方研究了这个问题,似乎很多解决方案都涉及添加int n(用于执行功能的数字)作为参数。

在编写类似的方法时,我们鼓励使用“this”作为我们试图改变的代表。通常情况下,“this”用于实例方法,即:this.increment()。

当我为它编写伪代码时,我发现我想要执行以下操作:

public void power(int p) {
    assert p >= 0 : "Violation of: p >= 0";
    int x = 0;

    if (p == 0) {
        return 0;
    }
    if (p % 2 == 0) {
        x = this.power(p / 2);
        return x * x;
    } else {
        return this * this.power(p - 1);
    }

我只是不知道如何编写它以便它可以实际运行eclipse和java。任何见解将不胜感激!

4 个答案:

答案 0 :(得分:2)

以下停止条件错误,应返回1

if (p == 0) {
  return 0; // should return 1
}

如果使用this,则不能在Java中使用乘法运算符*。你可以拥有一个不应该改变被叫方的multiplyBy函数:

public int multiplyBy(int x) {
    return this.value * x;
}

正如您在上面的方法中所看到的,您需要有一个实例属性来存储整数值。所以你的代码现在看起来像:

public void power(int p) {
  assert p >= 0 : "Violation of: p >= 0";
  int x = 0;

  if (p == 0) {
    return 1;
  }
  if (p % 2 == 0) {
    x = this.power(p / 2);
    return x * x;
  } else {
    return this.multiplyBy(this.power(p - 1));
  }
}

答案 1 :(得分:1)

进行递归时,你必须考虑两件事。

  • 基本情况:什么时候停止递归?
  • 递归案例:你什么时候继续?

对于电源,请考虑可以这样定义:

pow(x, p) = x * pow(x, p-1) if p > 0 else 1

原因是:

  • x 2 是x * x。
  • x 0 为1。

因此,考虑到这一点,让我们考虑一些边缘情况。我们不想接受任何对此方案不利的值,因此assert可以正常工作。最好为此使用例外,因为不是每个人都是going to enable assertions with the -ea flag

public long power(int x, int p) {
    if(p < 0) {
        throw new IllegalArgumentException("p is negative, not allowed");
    }
    if(p == 0) {
        return 1;
    } else {
        return x * power(x, p - 1);
    }
}

答案 2 :(得分:1)

我会创建方法static(所以你不需要实例)。传递您想要提升到幂和指数的值。像

这样的东西
public static long power(int n, int pow) {
    assert pow >= 0 : "Violation of: pow >= 0";
    if (pow == 0) { // <-- n^0 = 1
        return 1;
    } else if (pow == 1) { // <-- n^1 = n
        return n;
    }
    return n * power(n, pow - 1); // <-- recurse
}

作为实例方法

通常我会使用Math.pow(double, double)但是如果你真的需要一个就地递归实现,那么你可以做类似的事情

class Power {
    public Power(int n) {
        this.n = n;
    }
    private int n;
    private long result = -1;

    public String toString() {
        return String.valueOf(result);
    }

    public long getResult() {
        return result;
    }

    public long power(int pow) {
        assert pow >= 0 : "Violation of: pow >= 0";
        if (pow == 0) { // <-- n^0 = 1
            return this.result = 1;
        } else if (pow == 1) { // <-- n^1 = n
            return this.result = this.n;
        }
        return this.result = this.n * this.power(pow - 1); // <-- recurse
    }

    public void setN(int n) {
        this.n = n;
    }

    public int getN() {
        return n;
    }
}

答案 3 :(得分:0)

我猜你正在寻找这个。

class Ideone  
{   
  public static void main (String[] args) throws java.lang.Exception  
 {

    Test t = new Test(2);
    System.out.println(t.power(5));
 }
}


class Test
{
int n;
Test(int n)
{
    this.n = n;
}
public int power(int p) {
 int x = 0;

 if (p == 0) {
    return 1;
 }
 if (p == 1) {
     return this.n;
 }
 if (p % 2 == 0) {
    x = this.power(p / 2);
    return x * x;
 } 
 else {
    return this.n * this.power(p - 1);     
     }      
}}

您可以在https://ideone.com/0ghIVe处查看。