zoukankan      html  css  js  c++  java
  • 递归的几个经典例子

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

    递归的几个经典例子:
    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+"	");
      9              n--;
     10              iterator(n);
     11          }
     12 
     13     }
     14  }
     15 
     16 
     17 

  • 相关阅读:
    <JavaScript> 组合继承
    <JavaScript> 稳妥构造函数模式与工厂模式的区别
    <JavaScript> call()、apply()、bind() 的用法
    <JavaScript>可枚举属性与不可枚举属性
    <JavaScript>闭包(closure)
    在MongoDB中实现聚合函数
    (转)如何入门 Python 爬虫
    Python爬虫实战四之抓取淘宝MM照片
    转载:十年驾车经验总结:活着,才是硬道理
    设计模式之单例模式的七种写法
  • 原文地址:https://www.cnblogs.com/huguangqin/p/7128738.html
Copyright © 2011-2022 走看看