- /*
- 递归的应用
- 说明,目前只是懂了一点,还需要大牛多指导, 比如说更难一点递归应用
- */
- class Recursion {
- public static void main(String[] args) {
- System.out.println(recursionSum(4));
- System.out.println(recursion(4));
- }
- /*
- 定义getSum方法完成0-x的累加求和
- */
- public static int getSum(int x) {
- int sum = 0;
- for (int i = 0;i <= i ;i++ ) {
- sum += i;
- }
- return sum;
- }
- /*
- 用递归方式完成此功能
- */
- public static int recursionSum(int x) {
- int sum = 0;
- if (x == 1)
- return 1;
- sum = recursionSum(x-1);
- return sum + x ;
- }
-
- /*
- 深化,用递归算阶乘
- */
- public static int recursion(int x) {
- int sum = 1;
- if (x == 1)
- return 1;
- sum = recursion(x-1);
- return sum * x;
- }
-
- }
复制代码 |
|