Recursive method - Java

廉价感情. 提交于 2019-12-08 11:06:39

问题


Addition information:

Chip doesn't support multiplication, only addition. I should work around this problem by creating a recursive method, mult(), that performs multiplication of x and y by adding x to itself y times. Its arguments are x and y and its return value is the product of x and y. I should then write the method and a main() to call it.

It's pure logical thinking, but I get lost every time I try to think what to do.

I am stuck at the math part.. What I have, that doesn't work and I know the math is wrong, but I am not good at this:

public static void mult(int x, int y) {
    x = 0;
    y = 0;
    if (y > 0) {
        for (int i = 0; i < y; i++) {
            x = x * (x * y);
            return mult(x, y);
        }
    }
}

回答1:


When I hear "recursion", I expect to see two things:

  1. A function calling itself with modified arguments each time.
  2. A stopping condition right at the top that tells the function when to stop, avoiding an infinite stack.

So where are yours? Start with writing those down in words before you write code.




回答2:


One possibility is to use an accumulator which will store the current value of the multiplication. I replace missing statements by ??? :

public static void main(String []args){
        System.out.println(mult(2,5));
 }
    public static int mult(int x, int y) {
      if(???) return ???;
      else return multAcc(???,???,???);
    }

    private static int multAcc(int x, int y, int acc){
        if(???) return ???;
        else return multAcc(???, ???, ???);
    }



回答3:


... by adding x to itself y times.

You could actually do that, instead of multiplying. Oh, and maybe if you don't set both x and y to zero, you would have something to add ;-)

One last thing: If you want a recursive solution, you don't need the for-loop.




回答4:


Java has no TCO by design, so using recursion for linear (not tree-like) processes is very bad idea. Especially for such task, which will most likely become a bottleneck in your program. Use loop instead.

Oh, it must be recursive anyway? Looks like a homework task. Do it yourself then.




回答5:


All you need to remember is that a multiplication is a repeated addition (assuming that both operands are >= 0), so we have:

  • The base case is when y is zero
  • If y is not zero, then add x one more time, and subtract 1 from y

Notice that as long as y is positive, it'll eventually have a value of zero. So basically we keep adding x a total number of y times; this is what I mean:

public static int mult(int x, int y) {
    if (y == 0)
        return 0;
    return x + mult(x, y-1);
}

The same code can be written in a tail-recursive style, too - meaning: there's nothing to do after the recursive call returns, and this is important for certain languages that support a so-called tail-call optimization:

public static int mult(int x, int y, int accumulator) {
    if (y == 0)
        return accumulator;
    return mult(x, y-1, x + accumulator);
}

The above will get called as follows, noticing that the last parameter is always initialized in zero:

mult(10, 5, 0)
=> 50



回答6:


public static int mult(int x, int y) {
        if (y == 0) {
            return 0;
        }
        if (y > 0) {
            return x + mult(x, y - 1);
        } else {
            return -x + mult(x, y + 1);
        }
    }

this was the solution by the way



来源:https://stackoverflow.com/questions/19067013/recursive-method-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!