概念 代码 package lesson.l11_oop2;/*** Illustration** @author DengQing* @version 1.0* @datetime 2022/7/2 21:42* @function 递归:求n个数的和、乘积*/ public class Recursion {public static void main(String[] args) {Recursion recursion = new Recursion();System.out.println(recursion.getSum(5));System.out.println(recursion.getMultiply(5));System.out.println(recursion.getSequence(10));recursion.printFibonacci(10);}/*** @return int* @Author DengQing* @Date 2022/7/2 21:43* @Param [n]* @Function 求n个数的和*/public int getSum(int n) {if (n == 1) {return 1;} else {return n + getSum(n - 1);}}/*** @return int* @Author DengQing* @Date 2022/7/2 21:45* @Param [n]* @Function 求n个数的乘积*/public int getMultiply(int n) {if (n == 1) {return 1;} else {return n * getMultiply(n - 1);}}/*** @return int* @Author DengQing* @Date 2022/7/2 21:49* @Param [n]* @Function 数列* 已知有一个数列:f(0)=1,f(1)=4,f(n+2)=2*f(n+1)+f(n),* 其中n是大于0的整数,求f(10)的值。*/public int getSequence(int n) {if (n == 0) {return 1;} else if (n == 1) {return 4;} else { // 不能再f(n+2)-2*f(n+1),这样会栈溢出,要往下算return 2 * getSequence(n - 1) + getSequence(n - 2);}}/*** @return int* @Author DengQing* @Date 2022/7/2 21:59* @Param [n]* @Function 输入一个数据n, 计算斐波那契数列(Fibonacci)的第n个值* 1 1 2 3 5 8 13 21 34 55* 规律:一个数等于前两个数之和* 要求:计算斐波那契数列(Fibonacci)的第n个值,并将整个数列打印出来*/public int getFibonacci(int n) {if (n == 1 || n == 2) {return 1;} else {return getFibonacci(n - 1) + getFibonacci(n - 2);}}/*** @return void* @Author DengQing* @Date 2022/7/2 22:06* @Param [n]* @Function 打印斐波那契数列*/public void printFibonacci(int n) {for (int i = 1; i <= n; i++) {System.out.print(getFibonacci(i) + "\t");}System.out.println();} }