简体   繁体   English

如何让我的 function 递归? (爪哇)

[英]How to get my function to be recursive? (Java)

I'm having to make a recursive function that will receive a stack of "int" and output the sum of the squares of the elements in the stack.我必须制作一个递归的 function ,它将接收一个“int”堆栈和 output 堆栈中元素的平方和。 Here is what I have这是我所拥有的

public int sum_sqr_rec(Stack<Integer> stk){
int sum = 0;
for (int i=0; i<stk.size(); i++){
sum += (stk.get(i) * stk.get(i));
}
return sum;
}

The most important thing you need to determine for a recursive function is when to terminate it.对于递归 function,您需要确定的最重要的事情是何时终止它。

The second important thing to consider is what to return when you terminate it.第二个要考虑的重要事情是当你终止它时要返回什么。 When you start adding numbers, you start with sum = 0 .当您开始添加数字时,您从sum = 0开始。 From a recursive function, which is supposed to calculate the sum of numbers, the same value (ie 0 ) can be returned when you terminate it.从应该计算数字总和的递归 function 中,终止它时可以返回相同的值(即0 )。 Similarly, from a recursive function, which is supposed to return the product of numbers, you can return 1 on termination.类似地,对于递归 function,它应该返回数字的乘积,您可以在终止时返回1

import java.util.Stack;

public class Main {
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<Integer>();
        stack.add(2);
        stack.add(3);
        stack.add(4);
        stack.add(5);
        System.out.println(sum_sqr_rec(stack));
    }

    static int sum_sqr_rec(Stack<Integer> stk) {
        if (stk.isEmpty()) {
            return 0;
        }
        int n = stk.pop();
        return n * n + sum_sqr_rec(stk);
    }
}

Output: Output:

54

You can use recursion like this:您可以像这样使用递归:

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.add(2);
        stack.add(2);
        stack.add(4);
        stack.add(5);
        System.out.println(sum_sqr_rec(stack));
    }

    public static int sum_sqr_rec(Stack<Integer> stack) {
        if (stack.isEmpty())
            return 0;
        return stack.peek() * stack.pop() + sum_sqr_rec(stack);
    }

Note that I use Deque interface rather than Stack class directly (documentation says it should be used in preference to the Stack class).请注意,我直接使用 Deque 接口而不是 Stack class (文档说它应该优先使用 Stack 类)。

    public int recursive(Deque<Integer> stk){
        if (stk.Empty()){
            return 0;
        }
        return Math.pow(stack.pop(), 2) + recursive(stk);
    }

Lots of ways to do this.很多方法可以做到这一点。 But if you don't want to destroy the stack you can do it this way.但是如果你不想破坏堆栈,你可以这样做。 The stack is restored during the return process.堆栈在返回过程中被恢复。

  • n is popped. n被弹出。 This depletes the stack of numbers and keeps n in the local call stack for later use.这会耗尽数字堆栈并将n保留在本地调用堆栈中以供以后使用。
  • The square of the element is saved on the call stack of the method in k元素的平方保存在k中方法的调用栈上

  • r is initialized for the final tally. r被初始化为最终计数。

  • Once the stack is empty, simply push n back on stk and return the sums of the saved squares.一旦堆栈为空,只需将n推回stk并返回保存的平方和。
static int sum_sqr_rec(Stack<Integer> stk) {
      int n = stk.pop();
      int k = n*n;
      int r = 0;
      if (!stk.isEmpty()) {
            r = sum_sqr_rec(stk);
      }
      stk.push(n);
      return r + k;
}

Using this call sequence of statements.使用这个调用序列的语句。

System.out.println(stack);
System.out.println(sum_sqr_rec(stack));
System.out.println(stack);

Results in the following结果如下

[2, 3, 4, 5]
54
[2, 3, 4, 5]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM