如何使我的函数变成递归函数?(Java)

huangapple go评论98阅读模式
英文:

How to get my function to be recursive? (Java)

问题

我正在编写一个递归函数,它将接收一个存有 "int" 元素的栈,并输出栈中元素平方和的结果。以下是我的代码:

  1. public int sum_sqr_rec(Stack<Integer> stk) {
  2. int sum = 0;
  3. for (int i = 0; i < stk.size(); i++) {
  4. sum += (stk.get(i) * stk.get(i));
  5. }
  6. return sum;
  7. }
英文:

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.
Here is what I have

  1. public int sum_sqr_rec(Stack&lt;Integer&gt; stk){
  2. int sum = 0;
  3. for (int i=0; i&lt;stk.size(); i++){
  4. sum += (stk.get(i) * stk.get(i));
  5. }
  6. return sum;
  7. }

答案1

得分: 1

以下是翻译好的内容:

递归函数中最重要的是确定何时终止它。

第二个需要考虑的重要因素是在终止递归时返回什么。当你开始累加数字时,你可以从 sum = 0 开始。对于一个用于计算数字和的递归函数,在终止时可以返回相同的值(即 0)。同样地,对于一个用于计算数字乘积的递归函数,在终止时可以返回 1

  1. import java.util.Stack;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Stack<Integer> stack = new Stack<Integer>();
  5. stack.add(2);
  6. stack.add(3);
  7. stack.add(4);
  8. stack.add(5);
  9. System.out.println(sum_sqr_rec(stack));
  10. }
  11. static int sum_sqr_rec(Stack<Integer> stk) {
  12. if (stk.isEmpty()) {
  13. return 0;
  14. }
  15. int n = stk.pop();
  16. return n * n + sum_sqr_rec(stk);
  17. }
  18. }

输出:

  1. 54
英文:

The most important thing you need to determine for a recursive function is when to terminate it.

The second important thing to consider is what to return when you terminate it. When you start adding numbers, you start with sum = 0. From a recursive function, which is supposed to calculate the sum of numbers, the same value (i.e. 0) can be returned when you terminate it. Similarly, from a recursive function, which is supposed to return the product of numbers, you can return 1 on termination.

  1. import java.util.Stack;
  2. public class Main {
  3. public static void main(String[] args) {
  4. Stack&lt;Integer&gt; stack = new Stack&lt;Integer&gt;();
  5. stack.add(2);
  6. stack.add(3);
  7. stack.add(4);
  8. stack.add(5);
  9. System.out.println(sum_sqr_rec(stack));
  10. }
  11. static int sum_sqr_rec(Stack&lt;Integer&gt; stk) {
  12. if (stk.isEmpty()) {
  13. return 0;
  14. }
  15. int n = stk.pop();
  16. return n * n + sum_sqr_rec(stk);
  17. }
  18. }

Output:

  1. 54

答案2

得分: 0

你可以像这样使用递归:

  1. public static void main(String[] args) {
  2. Stack<Integer> stack = new Stack<>();
  3. stack.add(2);
  4. stack.add(2);
  5. stack.add(4);
  6. stack.add(5);
  7. System.out.println(sum_sqr_rec(stack));
  8. }
  9. public static int sum_sqr_rec(Stack<Integer> stack) {
  10. if (stack.isEmpty())
  11. return 0;
  12. return stack.peek() * stack.pop() + sum_sqr_rec(stack);
  13. }
英文:

You can use recursion like this:

  1. public static void main(String[] args) {
  2. Stack&lt;Integer&gt; stack = new Stack&lt;&gt;();
  3. stack.add(2);
  4. stack.add(2);
  5. stack.add(4);
  6. stack.add(5);
  7. System.out.println(sum_sqr_rec(stack));
  8. }
  9. public static int sum_sqr_rec(Stack&lt;Integer&gt; stack) {
  10. if (stack.isEmpty())
  11. return 0;
  12. return stack.peek() * stack.pop() + sum_sqr_rec(stack);
  13. }

答案3

得分: 0

注意,我使用的是Deque接口,而不是直接使用Stack类(文档建议优先使用Deque接口)。

  1. public int recursive(Deque<Integer> stk){
  2. if (stk.isEmpty()){
  3. return 0;
  4. }
  5. return (int) Math.pow(stk.pop(), 2) + recursive(stk);
  6. }
英文:

Note that I use Deque interface rather than Stack class directly (documentation says it should be used in preference to the Stack class).

  1. public int recursive(Deque&lt;Integer&gt; stk){
  2. if (stk.Empty()){
  3. return 0;
  4. }
  5. return Math.pow(stack.pop(), 2) + recursive(stk);
  6. }
  7. </details>
  8. # 答案4
  9. **得分**: 0
  10. 有很多种方法可以做到这一点。但是,如果您不想销毁堆栈,可以按照以下方式进行。堆栈在返回过程中恢复。
  11. - `n` 被弹出。这使数字堆栈减少,并将 `n` 保留在本地调用堆栈中以供以后使用。
  12. - 元素的平方保存在 `k` 中的方法调用堆栈中。
  13. - `r` 被初始化为最终总数。
  14. - 一旦堆栈为空,只需将 `n` 重新推回 `stk` 并返回保存的平方和。
  15. ```java
  16. static int sum_sqr_rec(Stack<Integer> stk) {
  17. int n = stk.pop();
  18. int k = n*n;
  19. int r = 0;
  20. if (!stk.isEmpty()) {
  21. r = sum_sqr_rec(stk);
  22. }
  23. stk.push(n);
  24. return r + k;
  25. }

使用以下语句的调用序列。

  1. System.out.println(stack);
  2. System.out.println(sum_sqr_rec(stack));
  3. System.out.println(stack);

结果如下

  1. [2, 3, 4, 5]
  2. 54
  3. [2, 3, 4, 5]
英文:

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. This depletes the stack of numbers and keeps n in the local call stack for later use.

  • The square of the element is saved on the call stack of the method in k

  • r is initialized for the final tally.

  • Once the stack is empty, simply push n back on stk and return the sums of the saved squares.

  1. static int sum_sqr_rec(Stack&lt;Integer&gt; stk) {
  2. int n = stk.pop();
  3. int k = n*n;
  4. int r = 0;
  5. if (!stk.isEmpty()) {
  6. r = sum_sqr_rec(stk);
  7. }
  8. stk.push(n);
  9. return r + k;
  10. }

Using this call sequence of statements.

  1. System.out.println(stack);
  2. System.out.println(sum_sqr_rec(stack));
  3. System.out.println(stack);

Results in the following

  1. [2, 3, 4, 5]
  2. 54
  3. [2, 3, 4, 5]
  4. </details>

huangapple
  • 本文由 发表于 2020年5月4日 05:39:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/61581977.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定