Java递归理解 [英] Java recursion Understanding

查看:36
本文介绍了Java递归理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我理解这个解决方案吗,帮助我的意思是帮助我遍历所有内容,以便像我一样愚蠢的人理解它.非常感谢:(

Can someone please help me understand this solution, and by help I mean help me iterate through everything so someone as stupid as I can understand it. Thank you so much :(

         public void recurse(int n, char one, char two, char three)
         {
               if(n == 1)
               System.out.println(n + " " + one + " " + two);
               else
               { 
                   recurse(n - 1, one, three, two);
                   System.out.println(n + " " + one + " " + two);
                }
          }

函数调用:

          recurse (4, 'A', 'B', 'C');

推荐答案

立即从另一个函数调用一个函数的想法暗示了函数调用自身的可能性.Java 中的函数调用机制支持这种可能性,称为递归.递归是一种强大的通用编程技术,是许多极其重要的计算应用程序的关键,从为信息处理提供基本支持的组合搜索和排序方法(第 4 章)到用于信号处理的快速傅立叶变换(第9).

The idea of calling one function from another immediately suggests the possibility of a function calling itself. The function-call mechanism in Java supports this possibility, which is known as recursion. Recursion is a powerful general-purpose programming technique, and is the key to numerous critically important computational applications, ranging from combinatorial search and sorting methods methods that provide basic support for information processing (Chapter 4) to the Fast Fourier Transform for signal processing (Chapter 9).

你的第一个递归程序.递归的HelloWorld是实现阶乘函数,由等式为正整数N定义

Your first recursive program. The HelloWorld for recursion is to implement the factorial function, which is defined for positive integers N by the equation

N! = N × (N-1) × (N-2) × ... × 2 × 1 

不!使用 for 循环很容易计算,但在 Factorial.java 中更简单的方法是使用以下递归函数:

N! is easy to compute with a for loop, but an even easier method in Factorial.java is to use the following recursive function:

public static int factorial(int N) { 
   if (N == 1) return 1; 
   return N * factorial(N-1); 
} 

你可以通过注意到 factorial() 返回 1 = 1 来说服自己它产生了想要的结果!当 N 为 1 并且如果它正确计算值

You can persuade yourself that it produces the desired result by noting that factorial() returns 1 = 1! when N is 1 and that if it properly computes the value

(N-1)! = (N-1) × (N-2) × ... × 2 × 1 

然后它正确计算值

N! = N × (N-1)! = N × (N-1) × (N-2) × ... × 2 × 1 

我们可以像跟踪任何函数调用序列一样跟踪这个计算.

We can trace this computation in the same way that we trace any sequence of function calls.

factorial(5) 
   factorial(4) 
      factorial(3) 
         factorial(2) 
            factorial(1) 
               return 1 
            return 2*1 = 2 
         return 3*2 = 6 
      return 4*6 = 24 
   return 5*24 = 120

我们的 factorial() 实现展示了每个递归函数所需的两个主要组件.

Our factorial() implementation exhibits the two main components that are required for every recursive function.

The base case returns a value without making any subsequent recursive calls. It does this for one or more special input values for which the function can be evaluated without recursion. For factorial(), the base case is N = 1.

The reduction step is the central part of a recursive function. It relates the function at one (or more) inputs to the function evaluated at one (or more) other inputs. Furthermore, the sequence of parameter values must converge to the base case. For factorial(), the reduction step is N * factorial(N-1) and N decreases by one for each call, so the sequence of parameter values converges to the base case of N = 1. 


Now in your case (n==1) is the base condition or terminating condition.
recurse(4,'A','B','C')
  recurse(3,'A','C','B')
     recurse(2,'A','B','C')
        recurse(1,'A','C','B')

        return : 1 A C
     return : 2 A B
   return : 3 A C
return : 4 A B

final output : 
1 A C
2 A B
3 A C
4 A B

这篇关于Java递归理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆