首页 > 代码库 > 递归的几个经典例子

递归的几个经典例子

注意:构造方法不可递归,否则是无限创建对象;

递归的几个经典例子:
1.HannoiTower

  1 import java.util.Scanner;
  2  public class HanoiTower{
  3      //level代表盘子个数;三个char类型代表柱子
  4     public static void moveDish(int level, char from, char inter, char to){
  5          if(level == 1){
  6              System.out.println(""+from+"移动盘子1号到"+to);
  7          }else{
  8 
  9              moveDish(level-1,from,to,inter);//调用自身
 10             System.out.println(""+from+"移动盘子"+level+"号到"+to);
 11              moveDish(level-1,inter,from,to);
 12          }
 13      }
 14 
 15     public static void main(String[] args){
 16          Scanner sc = new Scanner(System.in);
 17          System.out.println("请输入盘子个数");
 18          int n = sc.nextInt();
 19          moveDish(n,‘a‘,‘b‘,‘c‘);
 20      }
 21  }
 22 


2.sum

  1 import java.util.Scanner;
  2  public class Sum{
  3      public static void main(String[] args){
  4          Scanner sc = new Scanner(System.in);
  5          System.out.println("请输入n:");
  6          int n = sc.nextInt();
  7          int sum1 = sum(n);
  8         System.out.println(sum1;
  9      }
 10 
 11     public static int sum(int n){
 12          if(n == 1){
 13              return 1;
 14          }else{
 15              return n+sum(n-1);
 16          }
 17      }
 18  }
 19 


3.factorial

  1 import java.util.Scanner;
  2  public class Factorial{
  3      public static void main(String[] args){
  4          Scanner sc = new Scanner(System.in);
  5          System.out.println("请输入一个小于20的整数,我会帮你求出它的阶乘:");
  6          int n = sc.nextInt();
  7          int fac1 = fac(n);
  8          System.out.println(n+"的阶乘为:"+fac1);
  9          System.out.println("~看我棒不棒~~");
 10      }
 11 
 12     public static int fac(int n){
 13          if(n == 1){B
 14              return 1;
 15          }else{
 16              return n*fac(n-1);
 17          }
 18      }
 19  }
 20 



4.sumFactorial

  1 import java.util.Scanner;
  2  public class SumFactorial{
  3      public static void main(String[] args){
  4          Scanner sc = new Scanner(System.in);
  5          System.out.println("请输入一个小于20的整数:");
  6          int n = sc.nextInt();
  7          int sf = sumFac(n);
  8          System.out.println(sf);
  9      }
 10      //阶乘和的累加
 11     public static int sumFac(int n){
 12          if(n == 1){
 13              return 1;
 14          }else{
 15              return fac(n)+sumFac(n-1);
 16          }
 17      }
 18      //求阶乘
 19     public static int fac(int n){
 20          if(n == 1){
 21              return 1;
 22          }else{
 23              return n*fac(n-1);
 24          }
 25      }
 26  }

5.使用递归,遍历 1 至100之间的每个数字

  1 public class Number{
  2      public static void main(String[] args){
  3          iterator(100);
  4      }
  5 
  6     public static void iterator(int n){
  7          if(n >= 1){
  8              System.out.print(n+"\t");
  9              n--;
 10              iterator(n);
 11          }
 12 
 13     }
 14  }
 15 
 16 
 17 

递归的几个经典例子