zoukankan      html  css  js  c++  java
  • 4.递归

    递归的含义:递归,就是在运行的过程中调用自己。

          递归必须要有三个要素:

      ①、边界条件

      ②、递归前进段

      ③、递归返回段

      当边界条件不满足时,递归前进;当边界条件满足时,递归返回。

    1.求一个数的阶乘
    0!=1
    1!=1
    负数没有阶乘
    n! = n*(n-1)*(n-2)*......1

    /**
    * 0!=1 1!=1 * 负数没有阶乘,如果输入负数返回-1 * @param n * @return */ public static int getFactorial(int n){ if(n >= 0){
        //如果等于0直接返回1
    if(n==0){ System.out.println(n+"!=1"); return 1; }else{
           //如果不等于0,则自己调用自己 System.out.println(n);
    int temp = n*getFactorial(n-1); System.out.println(n+"!="+temp); return temp; } } return -1; }

    2.使用递归实现99乘法表
    public class DiGui {
    public static void chengfa(int i) {
    if (i == 1) {
    System.out.println("1*1=1");
    } else {
    chengfa(i - 1);
    for (int j = 1; j <= i; j++) {
    System.out.println(j + "*" + i + "=" + j * i + " ");
    }
    System.out.println(" ");
    }
    }

    public static void main(String[] args){
    chengfa(4);
    }
    }
  • 相关阅读:
    SessionAttributes注解
    数据格式化
    数据类型转换器
    线程的常用方法总结
    线程生命周期
    分析配置DispatcherServlet类时load-on-startup标签作用
    springMVC的执行请求过程
    MyBatis之动态SQL
    MyBatis实现
    Spring框架中的JDK与CGLib动态代理
  • 原文地址:https://www.cnblogs.com/zhiaijingming/p/10507849.html
Copyright © 2011-2022 走看看