ToB企服应用市场:ToB评测及商务社交产业平台

标题: 递归相关知识2 [打印本页]

作者: 麻花痒    时间: 2023-7-14 20:42
标题: 递归相关知识2
递归相关知识2

多路递归--斐波那契额数列
  1. import java.util.Arrays;
  2. //递归求斐波那契第n项
  3. public class feibonaqie {
  4.     public static int fibonacci(int n){
  5.         int []cache=new int[n+1];
  6.         Arrays.fill(cache,-1);
  7.         cache[0]=0;
  8.         cache[1]=1;//[0,1,-1,-1,-1,-1]
  9.         return f(n,cache);
  10.     }
  11.     public static int f(int n,int[]cache){
  12.      /*   if(n==0){
  13.             return 0;
  14.         }
  15.         if(n==1){
  16.             return 1;
  17.         }*/
  18.         if(cache[n]!=-1){
  19.             return cache[n];
  20.         }
  21.         int x=f(n-1,cache);
  22.         int y=f(n-2,cache);
  23.         cache[n]=x+y;
  24.         return cache[n];
  25.     }
  26.     /*public static void main(String[] args) {
  27.         int f=f(8);
  28.         System.out.println(f);
  29.     }*/
  30. }
复制代码
汉诺塔问题
  1. import java.util.LinkedList;
  2. public class hannuota {
  3.     static LinkedList<Integer> a=new LinkedList<>();//三根柱子
  4.     static LinkedList<Integer> b=new LinkedList<>();
  5.     static LinkedList<Integer> c=new LinkedList<>();
  6.     //3 2 1
  7.     static void init(int n){
  8.         for(int i=n;i>=1;i--){
  9.             a.addLast(i);
  10.         }
  11.     }
  12.      /*
  13.      Params:n-圆席个数
  14. a-源
  15. b-信
  16. c-目
  17.       */
  18.     static void move(int n,LinkedList<Integer> a,LinkedList<Integer> b,
  19.                      LinkedList<Integer> c){
  20.         if(n==0){
  21.             return;
  22.         }
  23.        move(n-1,a,c,b);
  24.         c.addLast(a.removeLast());//中间
  25.         print();
  26.         move(n-1,b,a,c);
  27.     }
  28.     public static void main(String[]args){
  29.         init(3);
  30.       /*  System.out.println(a);
  31.         System.out.println(b);
  32.         System.out.println(c);
  33.         b.addLast(a.removeLast());*/
  34.         print();
  35.         move(3,a,b,c);
  36.     }
  37.     private static void print() {
  38.         System.out.println("----------------------");
  39.         System.out.println(a);
  40.         System.out.println(b);
  41.         System.out.println(c);
  42.     }
  43. }
复制代码
爆栈问题
  1. //递归求和n+n-1+...+1
  2. public class baozhan {
  3.     //f(n)=f(n-2)+n;
  4.     public static long sum(long n){
  5.      if(n==1){
  6.          return 1;
  7.      }
  8.      return sum(n-1)+n;
  9.     }
  10.     public static void main(String[] args) {
  11.        /* System.out.println(sum(15000));*///爆栈
  12.     }
  13. }
复制代码
杨辉三角

第一种,时间复杂度最高,空间复杂度最高
  1. public class yanghuisanjiao {
  2.     private static int element(int i,int j){
  3.         if(j==0||i==j){
  4.             return 1;
  5.         }
  6.         return element(i-1,j-1)+element(i-1,j);
  7.     }
  8. private static void printSpace(int n,int i){
  9.         int num=(n-1-i)*2;
  10.      for (int j = 0; j < num; j++) {
  11.          System.out.print(" ");
  12.      }
  13. }
  14.     public static void print(int n){
  15.         for (int i = 0; i < n; i++) {
  16.             printSpace(n,i);
  17.             for (int j = 0; j <= i; j++) {
  18.                 System.out.printf("%-4d",element(i,j));
  19.             }
  20.             System.out.println();
  21.         }
  22.     }
  23.     public static void main(String[] args) {
  24.       /*  System.out.println(element(4,2));*/
  25.         print(5);
  26.     }
  27. }
复制代码
数据结构素数实现
  1. public class yanghuisanjiao1 {
  2. /*
  3. 优化1·使用二组数组记忆法
  4. Params:triangle-二维数组
  5. i-行坐标
  6. j-列坐标
  7. Returns:该坐标元素值
  8. */
  9.     private static int element1(int[][]triangle,int i,int j){
  10.         if(triangle[i][j]>0){
  11.             return triangle[i][j];
  12.         }
  13.         if(j==0||i==j){
  14.             triangle[i][j]=1;
  15.             return 1;
  16.         }
  17.         triangle[i][j]=element1(triangle,i-1,j-1)+element1(triangle,i-1,j);
  18.         return triangle[i][j];
  19.     }
  20. private static void printSpace(int n,int i){
  21.         int num=(n-1-i)*2;
  22.      for (int j = 0; j < num; j++) {
  23.          System.out.print(" ");
  24.      }
  25. }
  26.     public static void print(int n){
  27.         int[][]triangle=new int[n][];
  28.         for (int i = 0; i < n; i++) {//行
  29.             triangle[i]=new int[i+1];
  30.          /*   printSpace(n,i);*/
  31.             for (int j = 0; j <= i; j++) {
  32.                 System.out.printf("%-4d",element1(triangle,i,j));
  33.             }
  34.             System.out.println();
  35.         }
  36.     }
  37.     public static void main(String[] args) {
  38.       /*  System.out.println(element(4,2));*/
  39.         print(5);
  40.     }
  41. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4